in reply to pg_getcopydata failing to get copy data using documented syntax

The documentation says (emphasis mine):
The first argument to pg_getcopydata is the variable into which the data will be stored (this variable should not be undefined, or it may throw a warning, although it may be a reference)
It works fine if you change the line to a REFERENCE to @data, like:
1 while $dbh->pg_getcopydata(\$data[$record_count++]) >= 0;
You get a $record_count one higher than necessary.

FYI - you do not need to quote "$schema" or "$table", since you are in a qq().

            "Battle not with trolls, lest ye become a troll; and if you gaze into the Internet, the Internet gazes also into you."
        -Friedrich Nietzsche: A Dynamic Translation

  • Comment on Re: pg_getcopydata failing to get copy data using documented syntax
  • Download Code

Replies are listed 'Best First'.
Re^2: pg_getcopydata failing to get copy data using documented syntax
by bitvector (Initiate) on Dec 28, 2011 at 18:46 UTC

    Ah ha! Using a reference got it going, thanks! Do you know offhand why it gets a higher record count than necessary? I know I can just pop off the blank element, but if there's a more correct way to do it, I'd rather do that.

    The quoting around $schema and $table is because our relations are named things like 'Sales Rep', which will break if not double quoted, because Postgres will convert the relation name to lower case and/or throw a syntax error on the space. They're (the double quotes) meant to be passed in that context as literal double quotes, rather than to cause interpolation of the values in $schema and $table.

      $dbh->pg_getcopydata(\$data[$record_count++])
      This is a post-increment.

      When the last data record is retrieved, pg_getcopydata returns a Non-zero value, so, it is called one more time, and this time it returns NO data, but the return code is negative.

      So, that last call with no data increments the $record_count unnecessarily.

      Any attempt to repair this in-line loses the simplicity of the current code, so the best bet is to add a line "$record_count --", and add a comment as to why you are doing that.

      Re- Quoting the $schema, $table: Ouch! spaces in these names is just poor DB design. I agree with you - adding the quotes is good practice, just for these cases (which I had not thought of).

                  "Battle not with trolls, lest ye become a troll; and if you gaze into the Internet, the Internet gazes also into you."
              -Friedrich Nietzsche: A Dynamic Translation

        Makes sense. Thanks for the clarification. I suppose using pop(@data) is about as good as $record_count-- before the pg_getcopydata() line - unless you have a suggestion to the contrary...

        The original database was migrated from an Access app. One of my key projects here is to get it migrated to something with sane schema. Until then, there're all sorts of hoops like the quoting I have to jump through...

        Thanks again!