1

Running postgres 10, the following code returns 0 rows:

drop table if exists foo;
create table foo(t text);
with x as (
  insert into foo values ('t') returning 't' as t)
select foo.* from foo join x on foo.t = x.t;

I would expect it to return 1 row. Can someone explain what is going on?

2
  • interesting behavior, it is displaying row count 1 if I execute the query twice. And continuously displays one row less from total number of rows. Commented Oct 12, 2017 at 18:51
  • select * from x Commented Oct 12, 2017 at 18:51

1 Answer 1

1

The table foo is empty as the inserted row will be visible when the (whole) command completes.

See this statement in the documentation (and the following example):

The sub-statements in WITH are executed concurrently with each other and with the main query. Therefore, when using data-modifying statements in WITH, the order in which the specified updates actually happen is unpredictable. All the statements are executed with the same snapshot (see Chapter 13), so they cannot “see” one another's effects on the target tables.

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.