snippetsqlCritical
How to create a temporary table using VALUES in PostgreSQL
Viewed 0 times
postgresqlcreatetemporaryusinghowvaluestable
Problem
I am learning PostgreSQL and trying to figure out how to create a temporary table or a
I looked at the documentation for CREATE TABLE and it says
So, I wrote a simple test as follows:
But PostgreSQL (9.3) is complaining about
syntax error at or near "AS"
My questions are:
-
How can I fix the statement above?
-
How can I adapt it to be used in a
Thanks in advance.
WITH declaration that can be used in place of regular table, for debugging purposes. I looked at the documentation for CREATE TABLE and it says
VALUES can be used as a query but gives no example; the documentation for the VALUES clause linked therein does not have an example either?So, I wrote a simple test as follows:
DROP TABLE IF EXISTS lookup;
CREATE TEMP TABLE lookup (
key integer,
val numeric
) AS
VALUES (0,-99999), (1,100);But PostgreSQL (9.3) is complaining about
syntax error at or near "AS"
My questions are:
-
How can I fix the statement above?
-
How can I adapt it to be used in a
WITH block?Thanks in advance.
Solution
EDIT: I am leaving the original accepted answer as it is, but please note that the edit below, as suggested by a_horse_with_no_name, is the preferred method for creating a temporary table using VALUES.
If you just want to select from some values, rather than just creating a table and inserting into it, you can do something like:
To actually create a temporary table in a similar fashion, use:
EDIT: As pointed out by a_horse_with_no_name, in the docs it states that
Note, also from the comments by a_horse_with_no_name, and in the OP's original question, this includes a cast to the correct datatypes inside the values list and uses a CTE (WITH) statement.
Also, as pointed out in Evan Carrol's answer, in Postgres prior to version 12 a CTE query is always an optimization fence, ie, the CTE is always materialized. There are many good reasons for using CTEs, but there can be quite a significant performance hit, if not used carefully. There are, however, many instances where the optimization fence can actually enhance performance, so this is something to be aware of, not to blindly avoid.
If you just want to select from some values, rather than just creating a table and inserting into it, you can do something like:
WITH vals (k,v) AS (VALUES (0,-9999), (1, 100))
SELECT * FROM vals;To actually create a temporary table in a similar fashion, use:
WITH vals (k,v) AS (VALUES (0,-9999), (1, 100))
SELECT * INTO temporary table temp_table FROM vals;EDIT: As pointed out by a_horse_with_no_name, in the docs it states that
CREATE TABLE AS... is functionally similar to SELECT INTO ..., but that the former is a superset of the latter and that SELECT INTO is used in plpgslq for assigning a value to a temporary variable -- so it would fail in that case. Therefore, while the above examples are valid for plain SQL, the CREATE TABLE form should be preferred.CREATE TEMP TABLE temp_table AS
WITH t (k, v) AS (
VALUES
(0::int,-99999::numeric),
(1::int,100::numeric)
)
SELECT * FROM t;Note, also from the comments by a_horse_with_no_name, and in the OP's original question, this includes a cast to the correct datatypes inside the values list and uses a CTE (WITH) statement.
Also, as pointed out in Evan Carrol's answer, in Postgres prior to version 12 a CTE query is always an optimization fence, ie, the CTE is always materialized. There are many good reasons for using CTEs, but there can be quite a significant performance hit, if not used carefully. There are, however, many instances where the optimization fence can actually enhance performance, so this is something to be aware of, not to blindly avoid.
Code Snippets
WITH vals (k,v) AS (VALUES (0,-9999), (1, 100))
SELECT * FROM vals;WITH vals (k,v) AS (VALUES (0,-9999), (1, 100))
SELECT * INTO temporary table temp_table FROM vals;CREATE TEMP TABLE temp_table AS
WITH t (k, v) AS (
VALUES
(0::int,-99999::numeric),
(1::int,100::numeric)
)
SELECT * FROM t;Context
StackExchange Database Administrators Q#86724, answer score: 87
Revisions (0)
No revisions yet.