1

I have an array of tuples in PostgreSQL 17.

I couldn't get SELECT...INTO...FROM to work for some reason:

SELECT ARRAY[(1,2)] AS a INTO temp_table;
ERROR: column "a" has pseudo-type record[] 
SQL state: 42P16

To solve it, I had to convert it to text:

SELECT ARRAY[(1,2)] AS a, ARRAY[(1,2)]::text AS b
a
type record[]
b
type text
{"(1,2)"} {"(1,2)"}

Now I need to convert column b back to array of tuples, like column a. How can it be done?

3
  • 1) Do not use images for textual data, copy and paste as text. 2) There are two versions of SELECT INTO one inside plpgsql and one outside. Which one are you using and what is the issue when using it? Commented Jan 21 at 21:25
  • SELECT ARRAY[(1,2)] AS a INTO temp_table ERROR: column "a" has pseudo-type record[] SQL state: 42P16 Commented Jan 21 at 21:35
  • that was the issue. ARRAY[(1,2)]::text works it around, but then... Commented Jan 21 at 21:37

1 Answer 1

1

What you're calling a tuple is technically type record - the parenthesized thing is row composite value literal syntax, with the row keyword skipped. Since ::record[] syntax won't work, you can create a dummy table or type to work around that:
demo at db<>fiddle

create type dummy_type as(x int, y int);
create temp table temp_dummy_table(x int, y int);
SELECT ARRAY[(1,2)] AS a
     , ARRAY[(1,2)]::text AS b
     , '{"(1,2)"}'::dummy_type[]
     , '{"(1,2)"}'::temp_dummy_table[];
a b dummy_type temp_dummy_table
{"(1,2)"} {"(1,2)"} {"(1,2)"} {"(1,2)"}

Related:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.