From 8b26769bc441fffa8aad31dddc484c2f4043d2c9 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Tue, 6 Sep 2022 13:19:44 +1200 Subject: [PATCH] Fix an assortment of improper usages of string functions In a similar effort to f736e188c and 110d81728, fixup various usages of string functions where a more appropriate function is available and more fit for purpose. These changes include: 1. Use cstring_to_text_with_len() instead of cstring_to_text() when working with a StringInfoData and the length can easily be obtained. 2. Use appendStringInfoString() instead of appendStringInfo() when no formatting is required. 3. Use pstrdup(...) instead of psprintf("%s", ...) 4. Use pstrdup(...) instead of psprintf(...) (with no formatting) 5. Use appendPQExpBufferChar() instead of appendPQExpBufferStr() when the length of the string being appended is 1. 6. appendStringInfoChar() instead of appendStringInfo() when no formatting is required and string is 1 char long. 7. Use appendPQExpBufferStr(b, .) instead of appendPQExpBuffer(b, "%s", .) 8. Don't use pstrdup when it's fine to just point to the string constant. I (David) did find other cases of #8 but opted to use #4 instead as I wasn't certain enough that applying #8 was ok (e.g in hba.c) Author: Ranier Vilela, David Rowley Discussion: https://postgr.es/m/CAApHDvo2j2+RJBGhNtUz6BxabWWh2Jx16wMUMWKUjv70Ver1vg@mail.gmail.com --- contrib/hstore/hstore_io.c | 4 +- contrib/sepgsql/schema.c | 2 +- src/backend/access/brin/brin_minmax_multi.c | 10 +- src/backend/access/rmgrdesc/xlogdesc.c | 4 +- src/backend/jit/llvm/llvmjit.c | 2 +- src/backend/libpq/hba.c | 4 +- src/backend/postmaster/postmaster.c | 2 +- src/backend/replication/logical/tablesync.c | 4 +- src/backend/utils/adt/date.c | 2 +- src/backend/utils/adt/misc.c | 2 +- src/backend/utils/adt/ruleutils.c | 10 +- src/backend/utils/adt/timestamp.c | 2 +- src/bin/pg_amcheck/pg_amcheck.c | 46 +- src/bin/pg_dump/pg_dump.c | 444 ++++++++++---------- src/bin/pg_dump/pg_dumpall.c | 2 +- src/bin/pgbench/pgbench.c | 9 +- src/bin/psql/command.c | 4 +- src/bin/psql/describe.c | 8 +- 18 files changed, 277 insertions(+), 284 deletions(-) diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c index fb72bb6cfe458..6161df2790379 100644 --- a/contrib/hstore/hstore_io.c +++ b/contrib/hstore/hstore_io.c @@ -1325,7 +1325,7 @@ hstore_to_json_loose(PG_FUNCTION_ARGS) } appendStringInfoChar(&dst, '}'); - PG_RETURN_TEXT_P(cstring_to_text(dst.data)); + PG_RETURN_TEXT_P(cstring_to_text_with_len(dst.data, dst.len)); } PG_FUNCTION_INFO_V1(hstore_to_json); @@ -1370,7 +1370,7 @@ hstore_to_json(PG_FUNCTION_ARGS) } appendStringInfoChar(&dst, '}'); - PG_RETURN_TEXT_P(cstring_to_text(dst.data)); + PG_RETURN_TEXT_P(cstring_to_text_with_len(dst.data, dst.len)); } PG_FUNCTION_INFO_V1(hstore_to_jsonb); diff --git a/contrib/sepgsql/schema.c b/contrib/sepgsql/schema.c index fecd02f07a8e5..9e408cf57189c 100644 --- a/contrib/sepgsql/schema.c +++ b/contrib/sepgsql/schema.c @@ -84,7 +84,7 @@ sepgsql_schema_post_create(Oid namespaceId) * check db_schema:{create} */ initStringInfo(&audit_name); - appendStringInfo(&audit_name, "%s", quote_identifier(nsp_name)); + appendStringInfoString(&audit_name, quote_identifier(nsp_name)); sepgsql_avc_check_perms_label(ncontext, SEPG_CLASS_DB_SCHEMA, SEPG_DB_SCHEMA__CREATE, diff --git a/src/backend/access/brin/brin_minmax_multi.c b/src/backend/access/brin/brin_minmax_multi.c index c8f22d5d17f22..4415669719f1a 100644 --- a/src/backend/access/brin/brin_minmax_multi.c +++ b/src/backend/access/brin/brin_minmax_multi.c @@ -3063,7 +3063,7 @@ brin_minmax_multi_summary_out(PG_FUNCTION_ARGS) appendStringInfo(&str, "%s ... %s", a, b); - c = cstring_to_text(str.data); + c = cstring_to_text_with_len(str.data, str.len); astate_values = accumArrayResult(astate_values, PointerGetDatum(c), @@ -3095,15 +3095,9 @@ brin_minmax_multi_summary_out(PG_FUNCTION_ARGS) { Datum a; text *b; - StringInfoData str; - - initStringInfo(&str); a = FunctionCall1(&fmgrinfo, ranges_deserialized->values[idx++]); - - appendStringInfoString(&str, DatumGetCString(a)); - - b = cstring_to_text(str.data); + b = cstring_to_text(DatumGetCString(a)); astate_values = accumArrayResult(astate_values, PointerGetDatum(b), diff --git a/src/backend/access/rmgrdesc/xlogdesc.c b/src/backend/access/rmgrdesc/xlogdesc.c index 6fec4853f1903..3fd7185f2173a 100644 --- a/src/backend/access/rmgrdesc/xlogdesc.c +++ b/src/backend/access/rmgrdesc/xlogdesc.c @@ -319,9 +319,9 @@ XLogRecGetBlockRefInfo(XLogReaderState *record, bool pretty, *fpi_len += XLogRecGetBlock(record, block_id)->bimg_len; if (XLogRecBlockImageApply(record, block_id)) - appendStringInfo(buf, " FPW"); + appendStringInfoString(buf, " FPW"); else - appendStringInfo(buf, " FPW for WAL verification"); + appendStringInfoString(buf, " FPW for WAL verification"); } } } diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c index 6c72d43beb663..fd3eecf27d318 100644 --- a/src/backend/jit/llvm/llvmjit.c +++ b/src/backend/jit/llvm/llvmjit.c @@ -514,7 +514,7 @@ llvm_function_reference(LLVMJitContext *context, else if (basename != NULL) { /* internal function */ - funcname = psprintf("%s", basename); + funcname = pstrdup(basename); } else { diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c index 8f2b8c6b03a8f..4637426d62751 100644 --- a/src/backend/libpq/hba.c +++ b/src/backend/libpq/hba.c @@ -899,7 +899,7 @@ do { \ errmsg("missing entry at end of line"), \ errcontext("line %d of configuration file \"%s\"", \ line_num, IdentFileName))); \ - *err_msg = psprintf("missing entry at end of line"); \ + *err_msg = pstrdup("missing entry at end of line"); \ return NULL; \ } \ } while (0) @@ -912,7 +912,7 @@ do { \ errmsg("multiple values in ident field"), \ errcontext("line %d of configuration file \"%s\"", \ line_num, IdentFileName))); \ - *err_msg = psprintf("multiple values in ident field"); \ + *err_msg = pstrdup("multiple values in ident field"); \ return NULL; \ } \ } while (0) diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 1664fcee2a70f..e75611fdd54cf 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -4457,7 +4457,7 @@ BackendInitialize(Port *port) appendStringInfo(&ps_data, "%s ", port->user_name); if (!am_walsender) appendStringInfo(&ps_data, "%s ", port->database_name); - appendStringInfo(&ps_data, "%s", port->remote_host); + appendStringInfoString(&ps_data, port->remote_host); if (port->remote_port[0] != '\0') appendStringInfo(&ps_data, "(%s)", port->remote_port); diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c index 91ba49a14bd4e..8514835ff4c5a 100644 --- a/src/backend/replication/logical/tablesync.c +++ b/src/backend/replication/logical/tablesync.c @@ -769,7 +769,7 @@ fetch_remote_table_info(char *nspname, char *relname, foreach(lc, MySubscription->publications) { if (foreach_current_index(lc) > 0) - appendStringInfo(&pub_names, ", "); + appendStringInfoString(&pub_names, ", "); appendStringInfoString(&pub_names, quote_literal_cstr(strVal(lfirst(lc)))); } @@ -1062,7 +1062,7 @@ copy_table(Relation rel) appendStringInfoString(&cmd, quote_identifier(lrel.attnames[i])); } - appendStringInfo(&cmd, ") TO STDOUT"); + appendStringInfoString(&cmd, ") TO STDOUT"); } else { diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 081dfa2450f96..a2bdde0459445 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -96,7 +96,7 @@ anytime_typmodout(bool istz, int32 typmod) if (typmod >= 0) return psprintf("(%d)%s", (int) typmod, tz); else - return psprintf("%s", tz); + return pstrdup(tz); } diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c index d35b5d1f4fac7..6c45fd2007ad2 100644 --- a/src/backend/utils/adt/misc.c +++ b/src/backend/utils/adt/misc.c @@ -219,7 +219,7 @@ pg_tablespace_databases(PG_FUNCTION_ARGS) } if (tablespaceOid == DEFAULTTABLESPACE_OID) - location = psprintf("base"); + location = "base"; else location = psprintf("pg_tblspc/%u/%s", tablespaceOid, TABLESPACE_VERSION_DIRECTORY); diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 6594a9aac7056..2b7b1b0c0f699 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -1453,7 +1453,7 @@ pg_get_indexdef_worker(Oid indexrelid, int colno, appendStringInfoChar(&buf, ')'); if (idxrec->indnullsnotdistinct) - appendStringInfo(&buf, " NULLS NOT DISTINCT"); + appendStringInfoString(&buf, " NULLS NOT DISTINCT"); /* * If it has options, append "WITH (options)" @@ -2332,9 +2332,9 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, Anum_pg_constraint_confdelsetcols, &isnull); if (!isnull) { - appendStringInfo(&buf, " ("); + appendStringInfoString(&buf, " ("); decompile_column_index_array(val, conForm->conrelid, &buf); - appendStringInfo(&buf, ")"); + appendStringInfoChar(&buf, ')'); } break; @@ -2363,7 +2363,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, ((Form_pg_index) GETSTRUCT(indtup))->indnullsnotdistinct) appendStringInfoString(&buf, "NULLS NOT DISTINCT "); - appendStringInfoString(&buf, "("); + appendStringInfoChar(&buf, '('); /* Fetch and build target column list */ val = SysCacheGetAttr(CONSTROID, tup, @@ -3583,7 +3583,7 @@ pg_get_function_sqlbody(PG_FUNCTION_ARGS) ReleaseSysCache(proctup); - PG_RETURN_TEXT_P(cstring_to_text(buf.data)); + PG_RETURN_TEXT_P(cstring_to_text_with_len(buf.data, buf.len)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index 49cdb290ac289..021b760f4e58f 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -131,7 +131,7 @@ anytimestamp_typmodout(bool istz, int32 typmod) if (typmod >= 0) return psprintf("(%d)%s", (int) typmod, tz); else - return psprintf("%s", tz); + return pstrdup(tz); } diff --git a/src/bin/pg_amcheck/pg_amcheck.c b/src/bin/pg_amcheck/pg_amcheck.c index 3cff319f02579..fea35e4b14627 100644 --- a/src/bin/pg_amcheck/pg_amcheck.c +++ b/src/bin/pg_amcheck/pg_amcheck.c @@ -1509,7 +1509,7 @@ append_db_pattern_cte(PQExpBuffer buf, const PatternInfoArray *pia, have_values = true; appendPQExpBuffer(buf, "%s\n(%d, ", comma, pattern_id); appendStringLiteralConn(buf, info->db_regex, conn); - appendPQExpBufferStr(buf, ")"); + appendPQExpBufferChar(buf, ')'); comma = ","; } } @@ -1765,7 +1765,7 @@ append_rel_pattern_raw_cte(PQExpBuffer buf, const PatternInfoArray *pia, appendPQExpBufferStr(buf, ", true::BOOLEAN"); else appendPQExpBufferStr(buf, ", false::BOOLEAN"); - appendPQExpBufferStr(buf, ")"); + appendPQExpBufferChar(buf, ')'); comma = ","; } @@ -1972,15 +1972,15 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations, * selected above, filtering by exclusion patterns (if any) that match * btree index names. */ - appendPQExpBuffer(&sql, - ", index (oid, nspname, relname, relpages) AS (" - "\nSELECT c.oid, r.nspname, c.relname, c.relpages " - "FROM relation r" - "\nINNER JOIN pg_catalog.pg_index i " - "ON r.oid = i.indrelid " - "INNER JOIN pg_catalog.pg_class c " - "ON i.indexrelid = c.oid " - "AND c.relpersistence != 't'"); + appendPQExpBufferStr(&sql, + ", index (oid, nspname, relname, relpages) AS (" + "\nSELECT c.oid, r.nspname, c.relname, c.relpages " + "FROM relation r" + "\nINNER JOIN pg_catalog.pg_index i " + "ON r.oid = i.indrelid " + "INNER JOIN pg_catalog.pg_class c " + "ON i.indexrelid = c.oid " + "AND c.relpersistence != 't'"); if (opts.excludeidx || opts.excludensp) appendPQExpBufferStr(&sql, "\nINNER JOIN pg_catalog.pg_namespace n " @@ -2011,15 +2011,15 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations, * primary heap tables selected above, filtering by exclusion patterns * (if any) that match the toast index names. */ - appendPQExpBuffer(&sql, - ", toast_index (oid, nspname, relname, relpages) AS (" - "\nSELECT c.oid, 'pg_toast', c.relname, c.relpages " - "FROM toast t " - "INNER JOIN pg_catalog.pg_index i " - "ON t.oid = i.indrelid" - "\nINNER JOIN pg_catalog.pg_class c " - "ON i.indexrelid = c.oid " - "AND c.relpersistence != 't'"); + appendPQExpBufferStr(&sql, + ", toast_index (oid, nspname, relname, relpages) AS (" + "\nSELECT c.oid, 'pg_toast', c.relname, c.relpages " + "FROM toast t " + "INNER JOIN pg_catalog.pg_index i " + "ON t.oid = i.indrelid" + "\nINNER JOIN pg_catalog.pg_class c " + "ON i.indexrelid = c.oid " + "AND c.relpersistence != 't'"); if (opts.excludeidx) appendPQExpBufferStr(&sql, "\nLEFT OUTER JOIN exclude_pat ep " @@ -2044,9 +2044,9 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations, * matched in their own right, so we rely on UNION to deduplicate the * list. */ - appendPQExpBuffer(&sql, - "\nSELECT pattern_id, is_heap, is_btree, oid, nspname, relname, relpages " - "FROM ("); + appendPQExpBufferStr(&sql, + "\nSELECT pattern_id, is_heap, is_btree, oid, nspname, relname, relpages " + "FROM ("); appendPQExpBufferStr(&sql, /* Inclusion patterns that failed to match */ "\nSELECT pattern_id, is_heap, is_btree, " diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index d25709ad5f371..67b6d9079ebe3 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -2838,25 +2838,25 @@ dumpDatabase(Archive *fout) /* * Fetch the database-level properties for this database. */ - appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, " - "datdba, " - "pg_encoding_to_char(encoding) AS encoding, " - "datcollate, datctype, datfrozenxid, " - "datacl, acldefault('d', datdba) AS acldefault, " - "datistemplate, datconnlimit, "); + appendPQExpBufferStr(dbQry, "SELECT tableoid, oid, datname, " + "datdba, " + "pg_encoding_to_char(encoding) AS encoding, " + "datcollate, datctype, datfrozenxid, " + "datacl, acldefault('d', datdba) AS acldefault, " + "datistemplate, datconnlimit, "); if (fout->remoteVersion >= 90300) - appendPQExpBuffer(dbQry, "datminmxid, "); + appendPQExpBufferStr(dbQry, "datminmxid, "); else - appendPQExpBuffer(dbQry, "0 AS datminmxid, "); + appendPQExpBufferStr(dbQry, "0 AS datminmxid, "); if (fout->remoteVersion >= 150000) - appendPQExpBuffer(dbQry, "datlocprovider, daticulocale, datcollversion, "); + appendPQExpBufferStr(dbQry, "datlocprovider, daticulocale, datcollversion, "); else - appendPQExpBuffer(dbQry, "'c' AS datlocprovider, NULL AS daticulocale, NULL AS datcollversion, "); - appendPQExpBuffer(dbQry, - "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " - "shobj_description(oid, 'pg_database') AS description " - "FROM pg_database " - "WHERE datname = current_database()"); + appendPQExpBufferStr(dbQry, "'c' AS datlocprovider, NULL AS daticulocale, NULL AS datcollversion, "); + appendPQExpBufferStr(dbQry, + "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " + "shobj_description(oid, 'pg_database') AS description " + "FROM pg_database " + "WHERE datname = current_database()"); res = ExecuteSqlQueryForSingleRow(fout, dbQry->data); @@ -3398,10 +3398,10 @@ getBlobs(Archive *fout) pg_log_info("reading large objects"); /* Fetch BLOB OIDs, and owner/ACL data */ - appendPQExpBuffer(blobQry, - "SELECT oid, lomowner, lomacl, " - "acldefault('L', lomowner) AS acldefault " - "FROM pg_largeobject_metadata"); + appendPQExpBufferStr(blobQry, + "SELECT oid, lomowner, lomacl, " + "acldefault('L', lomowner) AS acldefault " + "FROM pg_largeobject_metadata"); res = ExecuteSqlQuery(fout, blobQry->data, PGRES_TUPLES_OK); @@ -3685,9 +3685,9 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables) printfPQExpBuffer(query, "SELECT pol.oid, pol.tableoid, pol.polrelid, pol.polname, pol.polcmd, "); if (fout->remoteVersion >= 100000) - appendPQExpBuffer(query, "pol.polpermissive, "); + appendPQExpBufferStr(query, "pol.polpermissive, "); else - appendPQExpBuffer(query, "'t' as polpermissive, "); + appendPQExpBufferStr(query, "'t' as polpermissive, "); appendPQExpBuffer(query, "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE " " pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(rolname) from pg_catalog.pg_roles WHERE oid = ANY(pol.polroles)), ', ') END AS polroles, " @@ -3912,23 +3912,23 @@ getPublications(Archive *fout, int *numPublications) /* Get the publications. */ if (fout->remoteVersion >= 130000) - appendPQExpBuffer(query, - "SELECT p.tableoid, p.oid, p.pubname, " - "p.pubowner, " - "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate, p.pubviaroot " - "FROM pg_publication p"); + appendPQExpBufferStr(query, + "SELECT p.tableoid, p.oid, p.pubname, " + "p.pubowner, " + "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate, p.pubviaroot " + "FROM pg_publication p"); else if (fout->remoteVersion >= 110000) - appendPQExpBuffer(query, - "SELECT p.tableoid, p.oid, p.pubname, " - "p.pubowner, " - "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate, false AS pubviaroot " - "FROM pg_publication p"); + appendPQExpBufferStr(query, + "SELECT p.tableoid, p.oid, p.pubname, " + "p.pubowner, " + "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate, false AS pubviaroot " + "FROM pg_publication p"); else - appendPQExpBuffer(query, - "SELECT p.tableoid, p.oid, p.pubname, " - "p.pubowner, " - "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, false AS pubtruncate, false AS pubviaroot " - "FROM pg_publication p"); + appendPQExpBufferStr(query, + "SELECT p.tableoid, p.oid, p.pubname, " + "p.pubowner, " + "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, false AS pubtruncate, false AS pubviaroot " + "FROM pg_publication p"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -4045,7 +4045,7 @@ dumpPublication(Archive *fout, const PublicationInfo *pubinfo) first = false; } - appendPQExpBufferStr(query, "'"); + appendPQExpBufferChar(query, '\''); if (pubinfo->pubviaroot) appendPQExpBufferStr(query, ", publish_via_partition_root = true"); @@ -4466,11 +4466,11 @@ getSubscriptions(Archive *fout) query = createPQExpBuffer(); /* Get the subscriptions in current database. */ - appendPQExpBuffer(query, - "SELECT s.tableoid, s.oid, s.subname,\n" - " s.subowner,\n" - " s.subconninfo, s.subslotname, s.subsynccommit,\n" - " s.subpublications,\n"); + appendPQExpBufferStr(query, + "SELECT s.tableoid, s.oid, s.subname,\n" + " s.subowner,\n" + " s.subconninfo, s.subslotname, s.subsynccommit,\n" + " s.subpublications,\n"); if (fout->remoteVersion >= 140000) appendPQExpBufferStr(query, " s.subbinary,\n"); @@ -5022,11 +5022,11 @@ getNamespaces(Archive *fout, int *numNamespaces) * we fetch all namespaces including system ones, so that every object we * read in can be linked to a containing namespace. */ - appendPQExpBuffer(query, "SELECT n.tableoid, n.oid, n.nspname, " - "n.nspowner, " - "n.nspacl, " - "acldefault('n', n.nspowner) AS acldefault " - "FROM pg_namespace n"); + appendPQExpBufferStr(query, "SELECT n.tableoid, n.oid, n.nspname, " + "n.nspowner, " + "n.nspacl, " + "acldefault('n', n.nspowner) AS acldefault " + "FROM pg_namespace n"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -5250,17 +5250,17 @@ getTypes(Archive *fout, int *numTypes) * cost of the subselect probe for all standard types. This would have to * be revisited if the backend ever allows renaming of array types. */ - appendPQExpBuffer(query, "SELECT tableoid, oid, typname, " - "typnamespace, typacl, " - "acldefault('T', typowner) AS acldefault, " - "typowner, " - "typelem, typrelid, " - "CASE WHEN typrelid = 0 THEN ' '::\"char\" " - "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, " - "typtype, typisdefined, " - "typname[0] = '_' AND typelem != 0 AND " - "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray " - "FROM pg_type"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, typname, " + "typnamespace, typacl, " + "acldefault('T', typowner) AS acldefault, " + "typowner, " + "typelem, typrelid, " + "CASE WHEN typrelid = 0 THEN ' '::\"char\" " + "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, " + "typtype, typisdefined, " + "typname[0] = '_' AND typelem != 0 AND " + "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray " + "FROM pg_type"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -5403,12 +5403,12 @@ getOperators(Archive *fout, int *numOprs) * system-defined operators at dump-out time. */ - appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, " - "oprnamespace, " - "oprowner, " - "oprkind, " - "oprcode::oid AS oprcode " - "FROM pg_operator"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, oprname, " + "oprnamespace, " + "oprowner, " + "oprkind, " + "oprcode::oid AS oprcode " + "FROM pg_operator"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -5477,10 +5477,10 @@ getCollations(Archive *fout, int *numCollations) * system-defined collations at dump-out time. */ - appendPQExpBuffer(query, "SELECT tableoid, oid, collname, " - "collnamespace, " - "collowner " - "FROM pg_collation"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, collname, " + "collnamespace, " + "collowner " + "FROM pg_collation"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -5545,10 +5545,10 @@ getConversions(Archive *fout, int *numConversions) * system-defined conversions at dump-out time. */ - appendPQExpBuffer(query, "SELECT tableoid, oid, conname, " - "connamespace, " - "conowner " - "FROM pg_conversion"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, conname, " + "connamespace, " + "conowner " + "FROM pg_conversion"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -5682,10 +5682,10 @@ getOpclasses(Archive *fout, int *numOpclasses) * system-defined opclasses at dump-out time. */ - appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, " - "opcnamespace, " - "opcowner " - "FROM pg_opclass"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, opcname, " + "opcnamespace, " + "opcowner " + "FROM pg_opclass"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -5750,10 +5750,10 @@ getOpfamilies(Archive *fout, int *numOpfamilies) * system-defined opfamilies at dump-out time. */ - appendPQExpBuffer(query, "SELECT tableoid, oid, opfname, " - "opfnamespace, " - "opfowner " - "FROM pg_opfamily"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, opfname, " + "opfnamespace, " + "opfowner " + "FROM pg_opfamily"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -5856,17 +5856,17 @@ getAggregates(Archive *fout, int *numAggs) } else { - appendPQExpBuffer(query, "SELECT tableoid, oid, proname AS aggname, " - "pronamespace AS aggnamespace, " - "pronargs, proargtypes, " - "proowner, " - "proacl AS aggacl, " - "acldefault('f', proowner) AS acldefault " - "FROM pg_proc p " - "WHERE proisagg AND (" - "pronamespace != " - "(SELECT oid FROM pg_namespace " - "WHERE nspname = 'pg_catalog')"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, proname AS aggname, " + "pronamespace AS aggnamespace, " + "pronargs, proargtypes, " + "proowner, " + "proacl AS aggacl, " + "acldefault('f', proowner) AS acldefault " + "FROM pg_proc p " + "WHERE proisagg AND (" + "pronamespace != " + "(SELECT oid FROM pg_namespace " + "WHERE nspname = 'pg_catalog')"); if (dopt->binary_upgrade) appendPQExpBufferStr(query, " OR EXISTS(SELECT 1 FROM pg_depend WHERE " @@ -6205,28 +6205,28 @@ getTables(Archive *fout, int *numTables) * wrong answers if any concurrent DDL is happening. */ - appendPQExpBuffer(query, - "SELECT c.tableoid, c.oid, c.relname, " - "c.relnamespace, c.relkind, c.reltype, " - "c.relowner, " - "c.relchecks, " - "c.relhasindex, c.relhasrules, c.relpages, " - "c.relhastriggers, " - "c.relpersistence, " - "c.reloftype, " - "c.relacl, " - "acldefault(CASE WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE) - " THEN 's'::\"char\" ELSE 'r'::\"char\" END, c.relowner) AS acldefault, " - "CASE WHEN c.relkind = " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN " - "(SELECT ftserver FROM pg_catalog.pg_foreign_table WHERE ftrelid = c.oid) " - "ELSE 0 END AS foreignserver, " - "c.relfrozenxid, tc.relfrozenxid AS tfrozenxid, " - "tc.oid AS toid, " - "tc.relpages AS toastpages, " - "tc.reloptions AS toast_reloptions, " - "d.refobjid AS owning_tab, " - "d.refobjsubid AS owning_col, " - "tsp.spcname AS reltablespace, "); + appendPQExpBufferStr(query, + "SELECT c.tableoid, c.oid, c.relname, " + "c.relnamespace, c.relkind, c.reltype, " + "c.relowner, " + "c.relchecks, " + "c.relhasindex, c.relhasrules, c.relpages, " + "c.relhastriggers, " + "c.relpersistence, " + "c.reloftype, " + "c.relacl, " + "acldefault(CASE WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE) + " THEN 's'::\"char\" ELSE 'r'::\"char\" END, c.relowner) AS acldefault, " + "CASE WHEN c.relkind = " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN " + "(SELECT ftserver FROM pg_catalog.pg_foreign_table WHERE ftrelid = c.oid) " + "ELSE 0 END AS foreignserver, " + "c.relfrozenxid, tc.relfrozenxid AS tfrozenxid, " + "tc.oid AS toid, " + "tc.relpages AS toastpages, " + "tc.reloptions AS toast_reloptions, " + "d.refobjid AS owning_tab, " + "d.refobjsubid AS owning_col, " + "tsp.spcname AS reltablespace, "); if (fout->remoteVersion >= 120000) appendPQExpBufferStr(query, @@ -6732,54 +6732,54 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) } appendPQExpBufferChar(tbloids, '}'); - appendPQExpBuffer(query, - "SELECT t.tableoid, t.oid, i.indrelid, " - "t.relname AS indexname, " - "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, " - "i.indkey, i.indisclustered, " - "c.contype, c.conname, " - "c.condeferrable, c.condeferred, " - "c.tableoid AS contableoid, " - "c.oid AS conoid, " - "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, " - "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, " - "t.reloptions AS indreloptions, "); + appendPQExpBufferStr(query, + "SELECT t.tableoid, t.oid, i.indrelid, " + "t.relname AS indexname, " + "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, " + "i.indkey, i.indisclustered, " + "c.contype, c.conname, " + "c.condeferrable, c.condeferred, " + "c.tableoid AS contableoid, " + "c.oid AS conoid, " + "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, " + "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, " + "t.reloptions AS indreloptions, "); if (fout->remoteVersion >= 90400) - appendPQExpBuffer(query, - "i.indisreplident, "); + appendPQExpBufferStr(query, + "i.indisreplident, "); else - appendPQExpBuffer(query, - "false AS indisreplident, "); + appendPQExpBufferStr(query, + "false AS indisreplident, "); if (fout->remoteVersion >= 110000) - appendPQExpBuffer(query, - "inh.inhparent AS parentidx, " - "i.indnkeyatts AS indnkeyatts, " - "i.indnatts AS indnatts, " - "(SELECT pg_catalog.array_agg(attnum ORDER BY attnum) " - " FROM pg_catalog.pg_attribute " - " WHERE attrelid = i.indexrelid AND " - " attstattarget >= 0) AS indstatcols, " - "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) " - " FROM pg_catalog.pg_attribute " - " WHERE attrelid = i.indexrelid AND " - " attstattarget >= 0) AS indstatvals, "); + appendPQExpBufferStr(query, + "inh.inhparent AS parentidx, " + "i.indnkeyatts AS indnkeyatts, " + "i.indnatts AS indnatts, " + "(SELECT pg_catalog.array_agg(attnum ORDER BY attnum) " + " FROM pg_catalog.pg_attribute " + " WHERE attrelid = i.indexrelid AND " + " attstattarget >= 0) AS indstatcols, " + "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) " + " FROM pg_catalog.pg_attribute " + " WHERE attrelid = i.indexrelid AND " + " attstattarget >= 0) AS indstatvals, "); else - appendPQExpBuffer(query, - "0 AS parentidx, " - "i.indnatts AS indnkeyatts, " - "i.indnatts AS indnatts, " - "'' AS indstatcols, " - "'' AS indstatvals, "); + appendPQExpBufferStr(query, + "0 AS parentidx, " + "i.indnatts AS indnkeyatts, " + "i.indnatts AS indnatts, " + "'' AS indstatcols, " + "'' AS indstatvals, "); if (fout->remoteVersion >= 150000) - appendPQExpBuffer(query, - "i.indnullsnotdistinct "); + appendPQExpBufferStr(query, + "i.indnullsnotdistinct "); else - appendPQExpBuffer(query, - "false AS indnullsnotdistinct "); + appendPQExpBufferStr(query, + "false AS indnullsnotdistinct "); /* * The point of the messy-looking outer join is to find a constraint that @@ -7002,13 +7002,13 @@ getExtendedStatistics(Archive *fout) query = createPQExpBuffer(); if (fout->remoteVersion < 130000) - appendPQExpBuffer(query, "SELECT tableoid, oid, stxname, " - "stxnamespace, stxowner, (-1) AS stxstattarget " - "FROM pg_catalog.pg_statistic_ext"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, " + "stxnamespace, stxowner, (-1) AS stxstattarget " + "FROM pg_catalog.pg_statistic_ext"); else - appendPQExpBuffer(query, "SELECT tableoid, oid, stxname, " - "stxnamespace, stxowner, stxstattarget " - "FROM pg_catalog.pg_statistic_ext"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, " + "stxnamespace, stxowner, stxstattarget " + "FROM pg_catalog.pg_statistic_ext"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -7729,15 +7729,15 @@ getEventTriggers(Archive *fout, int *numEventTriggers) query = createPQExpBuffer(); - appendPQExpBuffer(query, - "SELECT e.tableoid, e.oid, evtname, evtenabled, " - "evtevent, evtowner, " - "array_to_string(array(" - "select quote_literal(x) " - " from unnest(evttags) as t(x)), ', ') as evttags, " - "e.evtfoid::regproc as evtfname " - "FROM pg_event_trigger e " - "ORDER BY e.oid"); + appendPQExpBufferStr(query, + "SELECT e.tableoid, e.oid, evtname, evtenabled, " + "evtevent, evtowner, " + "array_to_string(array(" + "select quote_literal(x) " + " from unnest(evttags) as t(x)), ', ') as evttags, " + "e.evtfoid::regproc as evtfname " + "FROM pg_event_trigger e " + "ORDER BY e.oid"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -7809,15 +7809,15 @@ getProcLangs(Archive *fout, int *numProcLangs) int i_acldefault; int i_lanowner; - appendPQExpBuffer(query, "SELECT tableoid, oid, " - "lanname, lanpltrusted, lanplcallfoid, " - "laninline, lanvalidator, " - "lanacl, " - "acldefault('l', lanowner) AS acldefault, " - "lanowner " - "FROM pg_language " - "WHERE lanispl " - "ORDER BY oid"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, " + "lanname, lanpltrusted, lanplcallfoid, " + "laninline, lanvalidator, " + "lanacl, " + "acldefault('l', lanowner) AS acldefault, " + "lanowner " + "FROM pg_language " + "WHERE lanispl " + "ORDER BY oid"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -8768,10 +8768,10 @@ getTSDictionaries(Archive *fout, int *numTSDicts) query = createPQExpBuffer(); - appendPQExpBuffer(query, "SELECT tableoid, oid, dictname, " - "dictnamespace, dictowner, " - "dicttemplate, dictinitoption " - "FROM pg_ts_dict"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, dictname, " + "dictnamespace, dictowner, " + "dicttemplate, dictinitoption " + "FROM pg_ts_dict"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -8904,9 +8904,9 @@ getTSConfigurations(Archive *fout, int *numTSConfigs) query = createPQExpBuffer(); - appendPQExpBuffer(query, "SELECT tableoid, oid, cfgname, " - "cfgnamespace, cfgowner, cfgparser " - "FROM pg_ts_config"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, cfgname, " + "cfgnamespace, cfgowner, cfgparser " + "FROM pg_ts_config"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -8972,19 +8972,19 @@ getForeignDataWrappers(Archive *fout, int *numForeignDataWrappers) query = createPQExpBuffer(); - appendPQExpBuffer(query, "SELECT tableoid, oid, fdwname, " - "fdwowner, " - "fdwhandler::pg_catalog.regproc, " - "fdwvalidator::pg_catalog.regproc, " - "fdwacl, " - "acldefault('F', fdwowner) AS acldefault, " - "array_to_string(ARRAY(" - "SELECT quote_ident(option_name) || ' ' || " - "quote_literal(option_value) " - "FROM pg_options_to_table(fdwoptions) " - "ORDER BY option_name" - "), E',\n ') AS fdwoptions " - "FROM pg_foreign_data_wrapper"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, fdwname, " + "fdwowner, " + "fdwhandler::pg_catalog.regproc, " + "fdwvalidator::pg_catalog.regproc, " + "fdwacl, " + "acldefault('F', fdwowner) AS acldefault, " + "array_to_string(ARRAY(" + "SELECT quote_ident(option_name) || ' ' || " + "quote_literal(option_value) " + "FROM pg_options_to_table(fdwoptions) " + "ORDER BY option_name" + "), E',\n ') AS fdwoptions " + "FROM pg_foreign_data_wrapper"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -9063,17 +9063,17 @@ getForeignServers(Archive *fout, int *numForeignServers) query = createPQExpBuffer(); - appendPQExpBuffer(query, "SELECT tableoid, oid, srvname, " - "srvowner, " - "srvfdw, srvtype, srvversion, srvacl, " - "acldefault('S', srvowner) AS acldefault, " - "array_to_string(ARRAY(" - "SELECT quote_ident(option_name) || ' ' || " - "quote_literal(option_value) " - "FROM pg_options_to_table(srvoptions) " - "ORDER BY option_name" - "), E',\n ') AS srvoptions " - "FROM pg_foreign_server"); + appendPQExpBufferStr(query, "SELECT tableoid, oid, srvname, " + "srvowner, " + "srvfdw, srvtype, srvversion, srvacl, " + "acldefault('S', srvowner) AS acldefault, " + "array_to_string(ARRAY(" + "SELECT quote_ident(option_name) || ' ' || " + "quote_literal(option_value) " + "FROM pg_options_to_table(srvoptions) " + "ORDER BY option_name" + "), E',\n ') AS srvoptions " + "FROM pg_foreign_server"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -9167,17 +9167,17 @@ getDefaultACLs(Archive *fout, int *numDefaultACLs) * for the case of 'S' (DEFACLOBJ_SEQUENCE) which must be converted to * 's'. */ - appendPQExpBuffer(query, - "SELECT oid, tableoid, " - "defaclrole, " - "defaclnamespace, " - "defaclobjtype, " - "defaclacl, " - "CASE WHEN defaclnamespace = 0 THEN " - "acldefault(CASE WHEN defaclobjtype = 'S' " - "THEN 's'::\"char\" ELSE defaclobjtype END, " - "defaclrole) ELSE '{}' END AS acldefault " - "FROM pg_default_acl"); + appendPQExpBufferStr(query, + "SELECT oid, tableoid, " + "defaclrole, " + "defaclnamespace, " + "defaclobjtype, " + "defaclacl, " + "CASE WHEN defaclnamespace = 0 THEN " + "acldefault(CASE WHEN defaclobjtype = 'S' " + "THEN 's'::\"char\" ELSE defaclobjtype END, " + "defaclrole) ELSE '{}' END AS acldefault " + "FROM pg_default_acl"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -15491,7 +15491,7 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo) appendStringLiteralAH(q, qualrelname, fout); appendPQExpBufferStr(q, "::pg_catalog.regclass,"); appendStringLiteralAH(q, tbinfo->attnames[j], fout); - appendPQExpBufferStr(q, ","); + appendPQExpBufferChar(q, ','); appendStringLiteralAH(q, tbinfo->attmissingval[j], fout); appendPQExpBufferStr(q, ");\n\n"); } @@ -16361,11 +16361,11 @@ dumpConstraint(Archive *fout, const ConstraintInfo *coninfo) } else { - appendPQExpBuffer(q, "%s", - coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE"); + appendPQExpBufferStr(q, + coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE"); if (indxinfo->indnullsnotdistinct) - appendPQExpBuffer(q, " NULLS NOT DISTINCT"); - appendPQExpBuffer(q, " ("); + appendPQExpBufferStr(q, " NULLS NOT DISTINCT"); + appendPQExpBufferStr(q, " ("); for (k = 0; k < indxinfo->indnkeyattrs; k++) { int indkey = (int) indxinfo->indkeys[k]; diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index d665b257c9358..69ae027bd3815 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -978,7 +978,7 @@ dumpRoleMembership(PGconn *conn) "ug.rolname AS grantor, " "a.admin_option"); if (dump_inherit_option) - appendPQExpBuffer(buf, ", a.inherit_option"); + appendPQExpBufferStr(buf, ", a.inherit_option"); appendPQExpBuffer(buf, " FROM pg_auth_members a " "LEFT JOIN %s ur on ur.oid = a.roleid " "LEFT JOIN %s um on um.oid = a.member " diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 9aadcaad71e25..098fb43b3c7c4 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -3512,10 +3512,9 @@ printVerboseErrorMessages(CState *st, pg_time_usec_t *now, bool is_retry) resetPQExpBuffer(buf); printfPQExpBuffer(buf, "client %d ", st->id); - appendPQExpBuffer(buf, "%s", - (is_retry ? - "repeats the transaction after the error" : - "ends the failed transaction")); + appendPQExpBufferStr(buf, (is_retry ? + "repeats the transaction after the error" : + "ends the failed transaction")); appendPQExpBuffer(buf, " (try %u", st->tries); /* Print max_tries if it is not unlimitted. */ @@ -3532,7 +3531,7 @@ printVerboseErrorMessages(CState *st, pg_time_usec_t *now, bool is_retry) appendPQExpBuffer(buf, ", %.3f%% of the maximum time of tries was used", (100.0 * (*now - st->txn_scheduled) / latency_limit)); } - appendPQExpBuffer(buf, ")\n"); + appendPQExpBufferStr(buf, ")\n"); pg_log_info("%s", buf->data); } diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 61188d96f2a67..a141146e706bc 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -4889,9 +4889,9 @@ pset_value_string(const char *param, printQueryOpt *popt) else if (strcmp(param, "footer") == 0) return pstrdup(pset_bool_string(popt->topt.default_footer)); else if (strcmp(param, "format") == 0) - return psprintf("%s", _align2string(popt->topt.format)); + return pstrdup(_align2string(popt->topt.format)); else if (strcmp(param, "linestyle") == 0) - return psprintf("%s", get_line_style(&popt->topt)->name); + return pstrdup(get_line_style(&popt->topt)->name); else if (strcmp(param, "null") == 0) return pset_quoted_string(popt->nullPrint ? popt->nullPrint diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 327a69487bb9d..c645d66418a90 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -2148,9 +2148,9 @@ describeOneTableDetails(const char *schemaname, "SELECT inhparent::pg_catalog.regclass,\n" " pg_catalog.pg_get_expr(c.relpartbound, c.oid),\n "); - appendPQExpBuffer(&buf, - pset.sversion >= 140000 ? "inhdetachpending" : - "false as inhdetachpending"); + appendPQExpBufferStr(&buf, + pset.sversion >= 140000 ? "inhdetachpending" : + "false as inhdetachpending"); /* If verbose, also request the partition constraint definition */ if (verbose) @@ -2311,7 +2311,7 @@ describeOneTableDetails(const char *schemaname, printfPQExpBuffer(&tmpbuf, _("unique")); if (strcmp(indnullsnotdistinct, "t") == 0) appendPQExpBufferStr(&tmpbuf, _(" nulls not distinct")); - appendPQExpBuffer(&tmpbuf, _(", ")); + appendPQExpBufferStr(&tmpbuf, _(", ")); } else resetPQExpBuffer(&tmpbuf);