pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Year 2024: Extracting week from date gives wrong results for the last two days of the year
5+ messages / 5 participants
[nested] [flat]

* Year 2024: Extracting week from date gives wrong results for the last two days of the year
@ 2018-07-04 13:55 Michael Monscheuer <mm@digiperience.de>
  2018-07-04 14:13 ` Re: Year 2024: Extracting week from date gives wrong results for the last two days of the year Jeff Janes <jeff.janes@gmail.com>
  2018-07-04 14:14 ` Re: Year 2024: Extracting week from date gives wrong results for the last two days of the year Félix GERZAGUET <felix.gerzaguet@gmail.com>
  2018-07-04 17:21 ` Re: Year 2024: Extracting week from date gives wrong results for the last two days of the year Gavin Flower <GavinFlower@archidevsys.co.nz>
  0 siblings, 3 replies; 5+ messages in thread

From: Michael Monscheuer @ 2018-07-04 13:55 UTC (permalink / raw)
  To: pgsql-bugs

Team,

Extracting the week from a date gives wrong results for the last two days of 2024.

SELECT EXTRACT (WEEK FROM DATE '20241230');

or

SELECT EXTRACT (WEEK FROM DATE '20241231');

both result in a value of  1
which is wrong, obviously...

The expected result is 53

The results for the year 2020 are correct:
SELECT EXTRACT (WEEK FROM DATE '20201230');
results to 53

Version:
PostgreSQL 10.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit
on Amazon RDS

Greetings from Hamburg, DE

MiMo





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Year 2024: Extracting week from date gives wrong results for the last two days of the year
  2018-07-04 13:55 Year 2024: Extracting week from date gives wrong results for the last two days of the year Michael Monscheuer <mm@digiperience.de>
@ 2018-07-04 14:13 ` Jeff Janes <jeff.janes@gmail.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Jeff Janes @ 2018-07-04 14:13 UTC (permalink / raw)
  To: Michael Monscheuer <mm@digiperience.de>; +Cc: pgsql-bugs

On Wed, Jul 4, 2018 at 9:55 AM, Michael Monscheuer <mm@digiperience.de>
wrote:

> Team,
>
> Extracting the week from a date gives wrong results for the last two days
> of 2024.
>
> SELECT EXTRACT (WEEK FROM DATE '20241230');
>
> or
>
> SELECT EXTRACT (WEEK FROM DATE '20241231');
>
> both result in a value of  1
> which is wrong, obviously...
>
>
Not wrong.  See the docs:

By definition, ISO weeks start on Mondays and the first week of a year
contains January 4 of that year.

So the first week of 2025 contains some days from 2024.

Cheers,

Jeff

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Year 2024: Extracting week from date gives wrong results for the last two days of the year
  2018-07-04 13:55 Year 2024: Extracting week from date gives wrong results for the last two days of the year Michael Monscheuer <mm@digiperience.de>
@ 2018-07-04 14:14 ` Félix GERZAGUET <felix.gerzaguet@gmail.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Félix GERZAGUET @ 2018-07-04 14:14 UTC (permalink / raw)
  To: Michael Monscheuer <mm@digiperience.de>; +Cc: pgsql-bugs

On Wed, Jul 4, 2018 at 3:55 PM, Michael Monscheuer <mm@digiperience.de>
wrote:

> Extracting the week from a date gives wrong results for the last two days
> of 2024.
>
> SELECT EXTRACT (WEEK FROM DATE '20241230');
>
> or
>
> SELECT EXTRACT (WEEK FROM DATE '20241231');
>
> both result in a value of  1
> which is wrong, obviously...
>
> The expected result is 53
>

It is correct as per documentation (and the ISO 8601 standard):

Please look at: https://www.postgresql.org/docs/10/static/functions-
datetime.html
then looking for the week field you will see:

The number of the ISO 8601 week-numbering week of the year. By definition,
ISO weeks start on Mondays and the first week of a year contains January 4
of that year. In other words, the first Thursday of a year is in week 1 of
that year.

In the ISO week-numbering system, it is possible for early-January dates to
be part of the 52nd or 53rd week of the previous year, and for
late-December dates to be part of the first week of the next year. For
example, 2005-01-01 is part of the 53rd week of year 2004, and 2006-01-01
is part of the 52nd week of year 2005, while 2012-12-31 is part of the
first week of 2013. It's recommended to use the isoyear field together with
week to get consistent results.
Regards,

--
Félix

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Year 2024: Extracting week from date gives wrong results for the last two days of the year
  2018-07-04 13:55 Year 2024: Extracting week from date gives wrong results for the last two days of the year Michael Monscheuer <mm@digiperience.de>
@ 2018-07-04 17:21 ` Gavin Flower <GavinFlower@archidevsys.co.nz>
  2 siblings, 0 replies; 5+ messages in thread

From: Gavin Flower @ 2018-07-04 17:21 UTC (permalink / raw)
  To: Michael Monscheuer <mm@digiperience.de>; pgsql-bugs

On 05/07/18 01:55, Michael Monscheuer wrote:
> Team,
>
> Extracting the week from a date gives wrong results for the last two 
> days of 2024.
>
> SELECT EXTRACT (WEEK FROM DATE '20241230');
>
> or
>
> SELECT EXTRACT (WEEK FROM DATE '20241231');
>
> both result in a value of  1
> which is wrong, obviously...
>
> The expected result is 53
>
> The results for the year 2020 are correct:
> SELECT EXTRACT (WEEK FROM DATE '20201230');
> results to 53
>
> Version:
> PostgreSQL 10.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 
> 20140911 (Red Hat 4.8.3-9), 64-bit
> on Amazon RDS
>
> Greetings from Hamburg, DE
>
> MiMo
>
>
Weeks start on Monday.

Weeks belong to the year in which they have the most days.


Cheers,
Gavin





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* [PATCH] WIP: pg_upgrade --check: detect aggregates for pre-pg14
@ 2022-06-10 16:17 Justin Pryzby <pryzbyj@telsasoft.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Justin Pryzby @ 2022-06-10 16:17 UTC (permalink / raw)

This fails when upgrading from pre-14 (as expected), but it should fail during
pg_upgrade --check.

CREATE AGGREGATE array_accum(anyelement) (sfunc=array_append, stype=anyarray, initcond='{}');

See also:
9e38c2bb5093ceb0c04d6315ccd8975bd17add66
97f73a978fc1aca59c6ad765548ce0096d95a923
---
 src/bin/pg_upgrade/check.c | 97 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)

inline patch 1: src/bin/pg_upgrade/check.c (+97/-0 lines) download inline-1.patchdiff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c index ace7387edaf..4fd47e0b59a 100644 --- a/src/bin/pg_upgrade/check.c +++ b/src/bin/pg_upgrade/check.c @@ -31,6 +31,7 @@ static void check_for_jsonb_9_4_usage(ClusterInfo *cluster); static void check_for_pg_role_prefix(ClusterInfo *cluster); static void check_for_new_tablespace_dir(ClusterInfo *new_cluster); static void check_for_user_defined_encoding_conversions(ClusterInfo *cluster); +static void check_for_old_aggregates(ClusterInfo *cluster); static char *get_canonical_locale_name(int category, const char *locale); @@ -122,6 +123,12 @@ check_and_dump_old_cluster(bool live_check) if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300) check_for_user_defined_postfix_ops(&old_cluster); + /* + * Pre-PG 14 changed from anyarray to anycompatibelarray. + */ + if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300) + check_for_old_aggregates(&old_cluster); + /* * Pre-PG 12 allowed tables to be declared WITH OIDS, which is not * supported anymore. Verify there are none, iff applicable. @@ -775,6 +782,96 @@ check_proper_datallowconn(ClusterInfo *cluster) } +/* + * check_for_old_aggregates() + * + * Make sure there are no aggregates using anyelement which need to be changed + * to anycompatible. + */ +static void +check_for_old_aggregates(ClusterInfo *cluster) +{ + PGresult *res; + PGconn *conn = connectToServer(cluster, "template1"); + FILE *script = NULL; + char output_path[MAXPGPATH]; + + prep_status("Checking for old aggregates using anyelement"); + + snprintf(output_path, sizeof(output_path), "%s/%s", + log_opts.basedir, + "databases_with_old_aggregates.txt"); + + for (int dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) + { + bool db_used = false; + DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; + PGconn *conn = connectToServer(cluster, active_db->db_name); + int ntups; + int i_nspname, + i_proname; + + res = executeQueryOrDie(conn, + "SELECT pn.nspname, p.proname FROM " + "pg_proc p " + "JOIN pg_aggregate a ON a.aggfnoid=p.oid " + "JOIN pg_proc q ON q.oid=a.aggtransfn " + "JOIN pg_namespace pn ON pn.oid=p.pronamespace " + "JOIN pg_namespace qn ON qn.oid=q.pronamespace " + "WHERE pn.nspname != 'pg_catalog' " + "AND qn.nspname = 'pg_catalog' " + "AND 'anyelement'::regtype=ANY(q.proargtypes) " + "AND q.proname IN ('array_append'," + " 'array_prepend', 'array_cat', " + " 'array_position', 'array_positions', " + " 'array_remove', 'array_replace', " + " 'width_bucket');"); + // "AND aggtranstype='anyarray'::regtype + /* Before v11, used proisagg=true, and afterwards uses prokind='a' */ + + ntups = PQntuples(res); + + i_nspname = PQfnumber(res, "nspname"); + i_proname = PQfnumber(res, "proname"); + + for (int rowno = 0; rowno < ntups; rowno++) + { + if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL) + pg_fatal("could not open file \"%s\": %s\n", + output_path, strerror(errno)); + + if (!db_used) + { + db_used = true; + fprintf(script, "In database: %s\n", active_db->db_name); + } + + fprintf(script, " %s.%s\n", + PQgetvalue(res, rowno, i_nspname), + PQgetvalue(res, rowno, i_proname)); + } + + PQclear(res); + } + + PQfinish(conn); + + if (script) + { + fclose(script); + pg_log(PG_REPORT, "fatal\n"); + pg_fatal("The cluster contains aggregate functions using anyelement.\n" + "User defined aggregates which refer to built-in functions with\n" + "arguments of type 'anyelement' must be dropped before upgrading\n" + "and restored afterwards to refer to the corresponding functions\n" + "with arguments of type 'anycompatible'.\n" + "A list of the problem functions is in the file:\n" + " %s\n\n", output_path); + } + else + check_ok(); +} + /* * check_for_prepared_transactions() * -- 2.17.1
--98e8jtXdkpgskNou-- ^ permalink raw reply [nested|flat] 5+ messages in thread


end of thread, other threads:[~2022-06-10 16:17 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-07-04 13:55 Year 2024: Extracting week from date gives wrong results for the last two days of the year Michael Monscheuer <mm@digiperience.de>
2018-07-04 14:13 ` Jeff Janes <jeff.janes@gmail.com>
2018-07-04 14:14 ` Félix GERZAGUET <felix.gerzaguet@gmail.com>
2018-07-04 17:21 ` Gavin Flower <GavinFlower@archidevsys.co.nz>
2022-06-10 16:17 [PATCH] WIP: pg_upgrade --check: detect aggregates for pre-pg14 Justin Pryzby <pryzbyj@telsasoft.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox