* Dump table data using INSERT commands. * * Caution: when we restore from an archive file direct to database, the * INSERT commands emitted by this function have to be parsed by * pg_backup_db.c's ExecuteSimpleCommands(), which will not handle comments, * E'' strings, or dollar-quoted strings. So don't emit anything like that. */
| 2464 | * E'' strings, or dollar-quoted strings. So don't emit anything like that. |
| 2465 | */ |
| 2466 | static int |
| 2467 | dumpTableData_insert(Archive *fout, const void *dcontext) |
| 2468 | { |
| 2469 | TableDataInfo *tdinfo = (TableDataInfo *) dcontext; |
| 2470 | TableInfo *tbinfo = tdinfo->tdtable; |
| 2471 | DumpOptions *dopt = fout->dopt; |
| 2472 | PQExpBuffer q = createPQExpBuffer(); |
| 2473 | PQExpBuffer insertStmt = NULL; |
| 2474 | char *attgenerated; |
| 2475 | PGresult *res; |
| 2476 | int nfields, |
| 2477 | i; |
| 2478 | int rows_per_statement = dopt->dump_inserts; |
| 2479 | int rows_this_statement = 0; |
| 2480 | |
| 2481 | /* |
| 2482 | * If we're going to emit INSERTs with column names, the most efficient |
| 2483 | * way to deal with generated columns is to exclude them entirely. For |
| 2484 | * INSERTs without column names, we have to emit DEFAULT rather than the |
| 2485 | * actual column value --- but we can save a few cycles by fetching nulls |
| 2486 | * rather than the uninteresting-to-us value. |
| 2487 | */ |
| 2488 | attgenerated = (char *) pg_malloc(tbinfo->numatts * sizeof(char)); |
| 2489 | appendPQExpBufferStr(q, "DECLARE _pg_dump_cursor CURSOR FOR SELECT "); |
| 2490 | nfields = 0; |
| 2491 | for (i = 0; i < tbinfo->numatts; i++) |
| 2492 | { |
| 2493 | if (tbinfo->attisdropped[i]) |
| 2494 | continue; |
| 2495 | if (tbinfo->attgenerated[i] && dopt->column_inserts) |
| 2496 | continue; |
| 2497 | if (nfields > 0) |
| 2498 | appendPQExpBufferStr(q, ", "); |
| 2499 | if (tbinfo->attgenerated[i]) |
| 2500 | appendPQExpBufferStr(q, "NULL"); |
| 2501 | else |
| 2502 | appendPQExpBufferStr(q, fmtId(tbinfo->attnames[i])); |
| 2503 | attgenerated[nfields] = tbinfo->attgenerated[i]; |
| 2504 | nfields++; |
| 2505 | } |
| 2506 | /* Servers before 9.4 will complain about zero-column SELECT */ |
| 2507 | if (nfields == 0) |
| 2508 | appendPQExpBufferStr(q, "NULL"); |
| 2509 | appendPQExpBuffer(q, " FROM ONLY %s", |
| 2510 | fmtQualifiedDumpable(tbinfo)); |
| 2511 | if (tdinfo->filtercond) |
| 2512 | appendPQExpBuffer(q, " %s", tdinfo->filtercond); |
| 2513 | |
| 2514 | ExecuteSqlStatement(fout, q->data); |
| 2515 | |
| 2516 | while (1) |
| 2517 | { |
| 2518 | res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor", |
| 2519 | PGRES_TUPLES_OK); |
| 2520 | |
| 2521 | /* cross-check field count, allowing for dummy NULL if any */ |
| 2522 | if (nfields != PQnfields(res) && |
| 2523 | !(nfields == 0 && PQnfields(res) == 1)) |
nothing calls this directly
no test coverage detected