question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use BeginBinaryImport on table with a custom composite type

See original GitHub issue

Steps to reproduce

We have a table defined as follows:

CREATE TYPE my_custom_type AS (f1 int, f2 text);

CREATE TABLE aTable
(
    id serial primary key,
    custom_col my_custom_type
)

Running the following BeginBinaryImport results in an error message:

using (var writer = conn.BeginBinaryImport($"COPY \"public\".\"aTable\" (\"id\",\"custom_col\") FROM STDIN (FORMAT BINARY)"))
{
    writer.StartRow();
    writer.Write(1,"integer")
    writer.Write([composite value], "text")
    writer.Complete();
}

where [composite value] represents whatever I’m trying to insert for the composite value. I’ve tried formatting the value in many different ways such as:

'(1,"asdf")'
(1,"asdf")
ROW(1,"asdf")

This fails with the stack trace:

System.AggregateException: One or more errors occurred. (One or more errors occurred. (42804: wrong number of columns: 656956450, expected 1)) —> System.AggregateException: One or more errors occurred. (42804: wrong number of columns: 656956450, expected 1) —> Npgsql.PostgresException (0x80004005): 42804: wrong number of columns: 656956450, expected 1 at Npgsql.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|202_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage) at Npgsql.NpgsqlBinaryImporter.Complete(Boolean async, CancellationToken cancellationToken) at Npgsql.NpgsqlBinaryImporter.Complete()

Further technical details

Npgsql version: 5.0 PostgreSQL version: 9.6 Operating system: Windows

Other details about my project setup:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
akamorcommented, Jan 13, 2021

And btw, in case we never talk again I should at least thank you and all of the other maintainers for creating/supporting such a great product. We all really appreciate it!

1reaction
akamorcommented, Jan 13, 2021

Thanks for your help.

And yes, we represent the composite type as text when we first grab it from the users database by doing a type coercion to text, e.g. select id, custom_col::text from aTable;

At least this is our current plan and is working OK in our initial tests.

Interestingly, we do this for a few other types as well such as numeric and decimal as the CLR equivalent types don’t support the full range of values that Postgres supports.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not able to use created table as composite type - postgreSQL
I am trying to create a composite type in postgreSQl. In the documentation its given if we create a table a type for...
Read more >
Binary importing arrays of composite types in multiple ...
We ran into an issue related to #4365 with Npgsql 6.0.5 where binary importing arrays of composite types can fail if types with...
Read more >
Class NpgsqlBinaryImporter
Provides an API for a binary COPY FROM operation, a high-performance data import mechanism to a PostgreSQL table. Initiated by BeginBinaryImport(string) ...
Read more >
Bulk insert C# dataset to AWS PostgreSql-postgresql
Try using a library such as PgPartner to accomplish bulk additions very easily. ... Open(); // Execute BulkAdd by passing objects to insert...
Read more >
[Solved]-Bulk insert / copy iEnumerable into table with npgsql-C
I have written a class that behaves like the inbuilt SqlBulkCopy class for Postgres. It wraps the COPY command to provide fast uploads....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found