Order of operations in postgreSQL.

Started by Ken McClarenalmost 3 years ago6 messagesbugs
Jump to latest
#1Ken  McClaren
ken.mcclaren@kipuhealth.com

https://stackoverflow.com/questions/76339408/gracefully-handle-datatime-in-postgresql/76345292?noredirect=1#comment134659364_76345292

Just a few notes on this issue:

PSQL:
Appears to be applying the filter to all the columns in the table after the join.

TSQL:
Tried to duplicate this behavior and got these results.

select t1.id as t1id,
cast(cast(t1.test_value as varchar(max)) as datetime) as tms
from table1 t1
join table2 t2
on t1.id = t2.id
where cast(cast(t1.test_value as varchar(max)) as datetime) > '2023-05-27'

These statements work as expected in SQL Server.

Let me know if you have any questions.

[Logo Description automatically generated]<http://kipuhealth.com/&gt;
Ken McClaren
Database Administrator

864.313.5997
Easley, SC
Instagram<https://www.instagram.com/kipuhealth/&gt; | LinkedIn<https://www.linkedin.com/company/kipuhealth&gt; | Facebook<https://www.facebook.com/KipuSystems/&gt; | Twitter<https://twitter.com/kipuhealth&gt;
kipuhealth.com <http://kipuhealth.com/&gt;

Attachments:

image001.pngimage/png; name=image001.pngDownload
#2Wetmore, Matthew (CTR)
Matthew.Wetmore@express-scripts.com
In reply to: Ken McClaren (#1)
RE: Order of operations in postgreSQL.

From your stackoverflow example. (please don't post links, they will be hard to use in future if link expires)

This is what is happening.

Your code: TO_TIMESTAMP( trim ( substring(m.text_value, 1, 19)), 'YYYY-MM-DD"T"HH24:MI:SS' ) as tms

Your ERROR: ERROR: invalid value "test" for "YYYY"

Postgres logs are very clear about what is going on, if you step back and read them.

Test is in '', the error says 'test' is invalid for YYYY

Reproduced by:

SELECT TO_TIMESTAMP( trim ( substring('test00000000000000', 1, 19)), 'YYYY-MM-DD"T"HH24:MI:SS' )

Returns: ERROR: invalid value "test" for "YYYY" DETAIL: Value must be an integer.

From: Ken McClaren <ken.mcclaren@kipuhealth.com>
Sent: Wednesday, May 31, 2023 10:49 AM
To: pgsql-bugs@lists.postgresql.org
Cc: John Hall <john.hall@kipuhealth.com>
Subject: [EXTERNAL] Order of operations in postgreSQL.

https://stackoverflow.com/questions/76339408/gracefully-handle-datatime-in-postgresql/76345292?noredirect=1#comment134659364_76345292&lt;https://urldefense.com/v3/__https:/stackoverflow.com/questions/76339408/gracefully-handle-datatime-in-postgresql/76345292?noredirect=1*comment134659364_76345292__;Iw!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqLQJiRzg$&gt;

Just a few notes on this issue:

PSQL:
Appears to be applying the filter to all the columns in the table after the join.

TSQL:
Tried to duplicate this behavior and got these results.

select t1.id as t1id,
cast(cast(t1.test_value as varchar(max)) as datetime) as tms
from table1 t1
join table2 t2
on t1.id = t2.id
where cast(cast(t1.test_value as varchar(max)) as datetime) > '2023-05-27'

These statements work as expected in SQL Server.

Let me know if you have any questions.

[Logo Description automatically generated]<https://urldefense.com/v3/__http:/kipuhealth.com/__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqRvV4zTs$&gt;

Ken McClaren
Database Administrator

864.313.5997
Easley, SC
Instagram<https://urldefense.com/v3/__https:/www.instagram.com/kipuhealth/__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqgoUJt80$&gt; | LinkedIn<https://urldefense.com/v3/__https:/www.linkedin.com/company/kipuhealth__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFq4jhwZ7o$&gt; | Facebook<https://urldefense.com/v3/__https:/www.facebook.com/KipuSystems/__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqmAVwRsA$&gt; | Twitter<https://urldefense.com/v3/__https:/twitter.com/kipuhealth__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqhlgrdYI$&gt;
kipuhealth.com <https://urldefense.com/v3/__http:/kipuhealth.com/__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqRvV4zTs$&gt;

Attachments:

image002.pngimage/png; name=image002.pngDownload
#3Ken  McClaren
ken.mcclaren@kipuhealth.com
In reply to: Wetmore, Matthew (CTR) (#2)
Re: Order of operations in postgreSQL.

Yes, the error is correct, but it should never have occurred.
The join should have eliminated the error condition before it was evaluated by the where clause. That is the case when this type of statement is executed in SQL Server.

[Logo Description automatically generated]<http://kipuhealth.com/&gt;
Ken McClaren
Database Administrator

864.313.5997
Easley, SC
Instagram<https://www.instagram.com/kipuhealth/&gt; | LinkedIn<https://www.linkedin.com/company/kipuhealth&gt; | Facebook<https://www.facebook.com/KipuSystems/&gt; | Twitter<https://twitter.com/kipuhealth&gt;
kipuhealth.com <http://kipuhealth.com/&gt;

From: Wetmore, Matthew (CTR) <Matthew.Wetmore@express-scripts.com>
Date: Wednesday, May 31, 2023 at 4:42 PM
To: Ken McClaren <ken.mcclaren@kipuhealth.com>, pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Cc: John Hall <john.hall@kipuhealth.com>
Subject: RE: Order of operations in postgreSQL.

CAUTION: This email originated from outside the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
From your stackoverflow example. (please don’t post links, they will be hard to use in future if link expires)

This is what is happening.

Your code: TO_TIMESTAMP( trim ( substring(m.text_value, 1, 19)), 'YYYY-MM-DD"T"HH24:MI:SS' ) as tms

Your ERROR: ERROR: invalid value "test" for "YYYY"

Postgres logs are very clear about what is going on, if you step back and read them.

Test is in ‘’, the error says ‘test’ is invalid for YYYY

Reproduced by:

SELECT TO_TIMESTAMP( trim ( substring('test00000000000000', 1, 19)), 'YYYY-MM-DD"T"HH24:MI:SS' )

Returns: ERROR: invalid value "test" for "YYYY" DETAIL: Value must be an integer.

From: Ken McClaren <ken.mcclaren@kipuhealth.com>
Sent: Wednesday, May 31, 2023 10:49 AM
To: pgsql-bugs@lists.postgresql.org
Cc: John Hall <john.hall@kipuhealth.com>
Subject: [EXTERNAL] Order of operations in postgreSQL.

https://stackoverflow.com/questions/76339408/gracefully-handle-datatime-in-postgresql/76345292?noredirect=1#comment134659364_76345292&lt;https://urldefense.com/v3/__https:/stackoverflow.com/questions/76339408/gracefully-handle-datatime-in-postgresql/76345292?noredirect=1*comment134659364_76345292__;Iw!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqLQJiRzg$&gt;

Just a few notes on this issue:

PSQL:
Appears to be applying the filter to all the columns in the table after the join.

TSQL:
Tried to duplicate this behavior and got these results.

select t1.id as t1id,
cast(cast(t1.test_value as varchar(max)) as datetime) as tms
from table1 t1
join table2 t2
on t1.id = t2.id
where cast(cast(t1.test_value as varchar(max)) as datetime) > '2023-05-27'

These statements work as expected in SQL Server.

Let me know if you have any questions.

[Logo Description automatically generated]<https://urldefense.com/v3/__http:/kipuhealth.com/__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqRvV4zTs$&gt;
Ken McClaren
Database Administrator

864.313.5997
Easley, SC
Instagram<https://urldefense.com/v3/__https:/www.instagram.com/kipuhealth/__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqgoUJt80$&gt; | LinkedIn<https://urldefense.com/v3/__https:/www.linkedin.com/company/kipuhealth__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFq4jhwZ7o$&gt; | Facebook<https://urldefense.com/v3/__https:/www.facebook.com/KipuSystems/__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqmAVwRsA$&gt; | Twitter<https://urldefense.com/v3/__https:/twitter.com/kipuhealth__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqhlgrdYI$&gt;
kipuhealth.com <https://urldefense.com/v3/__http:/kipuhealth.com/__;!!GFE8dS6aclb0h1nkhPf9!8Gbv5mGohfEUVtNgQr_HkTXYFeKYHD0lMrw5Y6OTGKntYkX1Qx48MamQOTZ0rwUKgbMWIYRORorLk5i2akjNuSmiQZJf6SFqRvV4zTs$&gt;

Attachments:

image002.pngimage/png; name=image002.pngDownload
image001.pngimage/png; name=image001.pngDownload
#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Ken McClaren (#3)
Re: Order of operations in postgreSQL.

"Ken McClaren" <ken.mcclaren@kipuhealth.com> writes:

Yes, the error is correct, but it should never have occurred.
The join should have eliminated the error condition before it was evaluated by the where clause. That is the case when this type of statement is executed in SQL Server.

Postgres does not promise that JOIN conditions are evaluated before WHERE
conditions. I rather doubt that SQL Server does either, because it'd
cripple performance for a lot of real-world queries.

If you need something like that, you should restructure the query into two
levels with an optimization fence between them. One way is

SELECT ... FROM
(SELECT ... FROM t1 JOIN t2 ON join-condition OFFSET 0) ss
WHERE risky-where-condition

regards, tom lane

#5Ken  McClaren
ken.mcclaren@kipuhealth.com
In reply to: Tom Lane (#4)
Re: Order of operations in postgreSQL.

Thanks for your help.
This was a bad assumption on my part. I did not know that Postgres did not make that promise.

[Logo Description automatically generated]<http://kipuhealth.com/&gt;
Ken McClaren
Database Administrator

864.313.5997
Easley, SC
Instagram<https://www.instagram.com/kipuhealth/&gt; | LinkedIn<https://www.linkedin.com/company/kipuhealth&gt; | Facebook<https://www.facebook.com/KipuSystems/&gt; | Twitter<https://twitter.com/kipuhealth&gt;
kipuhealth.com <http://kipuhealth.com/&gt;

From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thursday, June 1, 2023 at 9:51 AM
To: Ken McClaren <ken.mcclaren@kipuhealth.com>
Cc: Wetmore, Matthew (CTR) <Matthew.Wetmore@express-scripts.com>, pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>, John Hall <john.hall@kipuhealth.com>
Subject: Re: Order of operations in postgreSQL.
[You don't often get email from tgl@sss.pgh.pa.us. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

CAUTION: This email originated from outside the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

"Ken McClaren" <ken.mcclaren@kipuhealth.com> writes:

Yes, the error is correct, but it should never have occurred.
The join should have eliminated the error condition before it was evaluated by the where clause. That is the case when this type of statement is executed in SQL Server.

Postgres does not promise that JOIN conditions are evaluated before WHERE
conditions. I rather doubt that SQL Server does either, because it'd
cripple performance for a lot of real-world queries.

If you need something like that, you should restructure the query into two
levels with an optimization fence between them. One way is

SELECT ... FROM
(SELECT ... FROM t1 JOIN t2 ON join-condition OFFSET 0) ss
WHERE risky-where-condition

regards, tom lane

Attachments:

image001.pngimage/png; name=image001.pngDownload
#6Josh Innis
joshinnis@gmail.com
In reply to: Ken McClaren (#5)
Re: Order of operations in postgreSQL.

On Thu, Jun 1, 2023 at 11:40 PM Ken McClaren <ken.mcclaren@kipuhealth.com>
wrote:

Show quoted text

Thanks for your help.

This was a bad assumption on my part. I did not know that Postgres did not
make that promise.

[image: Logo Description automatically generated] <http://kipuhealth.com/&gt;

*Ken McClaren*
Database Administrator

864.313.5997

Easley, SC

Instagram <https://www.instagram.com/kipuhealth/&gt; | LinkedIn
<https://www.linkedin.com/company/kipuhealth&gt; | Facebook
<https://www.facebook.com/KipuSystems/&gt; | Twitter
<https://twitter.com/kipuhealth&gt;

kipuhealth.com <http://kipuhealth.com/&gt;

*From: *Tom Lane <tgl@sss.pgh.pa.us>
*Date: *Thursday, June 1, 2023 at 9:51 AM
*To: *Ken McClaren <ken.mcclaren@kipuhealth.com>
*Cc: *Wetmore, Matthew (CTR) <Matthew.Wetmore@express-scripts.com>,
pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>, John
Hall <john.hall@kipuhealth.com>
*Subject: *Re: Order of operations in postgreSQL.

[You don't often get email from tgl@sss.pgh.pa.us. Learn why this is
important at https://aka.ms/LearnAboutSenderIdentification ]

CAUTION: This email originated from outside the organization. Do not click
links or open attachments unless you recognize the sender and know the
content is safe.

"Ken McClaren" <ken.mcclaren@kipuhealth.com> writes:

Yes, the error is correct, but it should never have occurred.
The join should have eliminated the error condition before it was

evaluated by the where clause. That is the case when this type of statement
is executed in SQL Server.

Postgres does not promise that JOIN conditions are evaluated before WHERE
conditions. I rather doubt that SQL Server does either, because it'd
cripple performance for a lot of real-world queries.

If you need something like that, you should restructure the query into two
levels with an optimization fence between them. One way is

SELECT ... FROM
(SELECT ... FROM t1 JOIN t2 ON join-condition OFFSET 0) ss
WHERE risky-where-condition

regards, tom lane

Attachments:

image001.pngimage/png; name=image001.pngDownload