Re: Array of C integers to temporary table?

From: "Obe, Regina" <robe(dot)dnd(at)cityofboston(dot)gov>
To: <postgres-novice(at)coreland(dot)ath(dot)cx>, <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Array of C integers to temporary table?
Date: 2009-01-27 19:47:22
Message-ID: 53F9CF533E1AA14EA1F8C5C08ABC08D205547E8D@ZDND.DND.boston.cob
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


> Hello.

> What's the "correct" (read: simple, efficient) way to
> pass an arbitrarily sized array of C integers to postgres
> and have it turned into a temporary table?

> I'm using PostgreSQL 7.4.

> I could, of course, turn the array into a long list of
> insert statements:

> BEGIN;
> CREATE TEMPORARY TABLE temp_table (id integer) ON COMMIT DROP;
> INSERT INTO temp_table VALUES (1);
> INSERT INTO temp_table VALUES (23);
> INSERT INTO temp_table VALUES (3889);
...

> But that seems long winded and rather inefficient.

> Any help appreciated!

I'm not quite sure what you mean by an array of C integers. You mean
you have an array in C
that you are trying to bring into PostgreSQL, or a random assortment of
integers in C you would like to bring in,
or you have a PostgreSQL array.

For a PostgreSQL array, I recall this working even in PostgreSQL 7.4

SELECT ary[i] AS c_val
FROM generate_series(1, array_upper(ary,0)) As i

Where ary is the name of your array.

You can then use the above as a subselect in another query

SELECT foo.*
FROM (SELECT ary[i] AS c_val
FROM generate_series(1, array_upper(ary,0)) As i) As foo

or dump into a temp table

INSERT INTO temp_table(id)
SELECT ary[i] AS c_val
FROM generate_series(1, array_upper(ary,1)) As i

So a hard-coded example

SELECT (ARRAY[1,2,3])[i] AS c_val
FROM generate_series(1, array_upper(ARRAY[1,2,3],1)) As i

Hope that helps,
Regina
-----------------------------------------
The substance of this message, including any attachments, may be
confidential, legally privileged and/or exempt from disclosure
pursuant to Massachusetts law. It is intended
solely for the addressee. If you received this in error, please
contact the sender and delete the material from any computer.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message postgres-novice 2009-01-27 20:44:25 Re: Array of C integers to temporary table?
Previous Message Tom Lane 2009-01-27 19:36:02 Re: Array of C integers to temporary table?