/* * libpq_testclient.c * A test program for the libpq public API * * Copyright (c) 2022-2026, PostgreSQL Global Development Group * * IDENTIFICATION * src/interfaces/libpq/test/libpq_testclient.c */ #include "postgres_fe.h" #include "libpq-fe.h" static void clear_result(PGresult *res) { if (res) PQclear(res); } static int fail_command_result(const char *label, PGconn *conn, PGresult *res) { fprintf(stderr, "%s failed: %s\n", label, res ? PQresultErrorMessage(res) : PQerrorMessage(conn)); clear_result(res); return 1; } static int check_command_result(const char *label, PGresult *res, const char *expected_status, const char *expected_tuples) { const char *actual_status = PQcmdStatus(res); const char *actual_tuples = PQcmdTuples(res); if (strcmp(actual_status, expected_status) != 0) { fprintf(stderr, "%s: expected PQcmdStatus=\"%s\", got \"%s\"\n", label, expected_status, actual_status); return 1; } if (strcmp(actual_tuples, expected_tuples) != 0) { fprintf(stderr, "%s: expected PQcmdTuples=\"%s\", got \"%s\"\n", label, expected_tuples, actual_tuples); return 1; } return 0; } static int check_single_value(PGconn *conn, const char *label, const char *sql, const char *expected_value) { PGresult *res = PQexec(conn, sql); const char *actual_value; if (PQresultStatus(res) != PGRES_TUPLES_OK) return fail_command_result(label, conn, res); if (PQntuples(res) != 1 || PQnfields(res) != 1) { fprintf(stderr, "%s: expected one row/one column result\n", label); clear_result(res); return 1; } actual_value = PQgetvalue(res, 0, 0); if (strcmp(actual_value, expected_value) != 0) { fprintf(stderr, "%s: expected result \"%s\", got \"%s\"\n", label, expected_value, actual_value); clear_result(res); return 1; } clear_result(res); return 0; } static int run_explain_dml_completion(const char *conninfo) { PGconn *conn = PQconnectdb(conninfo); PGresult *res = NULL; int rc = 1; if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "connection failed: %s\n", PQerrorMessage(conn)); goto done; } res = PQexec(conn, "CREATE TEMP TABLE one_c_explain_dml_t (a int, b int)"); if (PQresultStatus(res) != PGRES_COMMAND_OK) goto fail_result; clear_result(res); res = NULL; res = PQexec(conn, "EXPLAIN (COSTS OFF) " "INSERT INTO one_c_explain_dml_t VALUES (99,99)"); if (PQresultStatus(res) != PGRES_TUPLES_OK) goto fail_result; if (check_command_result("plain EXPLAIN INSERT", res, "EXPLAIN", "")) goto done; clear_result(res); res = NULL; if (check_single_value(conn, "plain EXPLAIN INSERT execution check", "SELECT count(*) FROM one_c_explain_dml_t WHERE a = 99", "0")) goto done; res = PQexec(conn, "EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) " "INSERT INTO one_c_explain_dml_t " "SELECT g, g FROM generate_series(1, 3) AS g"); if (PQresultStatus(res) != PGRES_TUPLES_OK) goto fail_result; if (check_command_result("EXPLAIN ANALYZE INSERT", res, "INSERT 0 3", "3")) goto done; clear_result(res); res = NULL; res = PQexec(conn, "INSERT INTO one_c_explain_dml_t VALUES (10,10)"); if (PQresultStatus(res) != PGRES_COMMAND_OK) goto fail_result; clear_result(res); res = NULL; res = PQexec(conn, "EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) " "UPDATE one_c_explain_dml_t " "SET b = b + 100 WHERE a IN (1, 2, 3, 10)"); if (PQresultStatus(res) != PGRES_TUPLES_OK) goto fail_result; if (check_command_result("EXPLAIN ANALYZE UPDATE", res, "UPDATE 4", "4")) goto done; clear_result(res); res = NULL; res = PQexec(conn, "EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) " "UPDATE one_c_explain_dml_t SET b = b + 1 WHERE a = -1"); if (PQresultStatus(res) != PGRES_TUPLES_OK) goto fail_result; if (check_command_result("EXPLAIN ANALYZE zero-row UPDATE", res, "UPDATE 0", "0")) goto done; clear_result(res); res = NULL; res = PQexec(conn, "EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) " "DELETE FROM one_c_explain_dml_t WHERE a IN (1, 2)"); if (PQresultStatus(res) != PGRES_TUPLES_OK) goto fail_result; if (check_command_result("EXPLAIN ANALYZE DELETE", res, "DELETE 2", "2")) goto done; clear_result(res); res = NULL; res = PQexec(conn, "EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) " "SELECT * FROM one_c_explain_dml_t WHERE a > 0"); if (PQresultStatus(res) != PGRES_TUPLES_OK) goto fail_result; if (check_command_result("EXPLAIN ANALYZE SELECT", res, "EXPLAIN", "")) goto done; clear_result(res); res = NULL; printf("EXPLAIN_DML_COMPLETION PASS\n"); rc = 0; goto done; fail_result: rc = fail_command_result("PQexec", conn, res); res = NULL; done: clear_result(res); PQfinish(conn); return rc; } static void print_ssl_library(void) { const char *lib = PQsslAttribute(NULL, "library"); if (!lib) fprintf(stderr, "SSL is not enabled\n"); else printf("%s\n", lib); } int main(int argc, char *argv[]) { if ((argc > 1) && !strcmp(argv[1], "--ssl")) { print_ssl_library(); return 0; } if ((argc == 3) && !strcmp(argv[1], "--explain-dml-completion")) return run_explain_dml_completion(argv[2]); printf("currently only --ssl and --explain-dml-completion are supported\n"); return 1; }