标签: postgresql

PostgreSQL - 在 REGEX_REPLACE() 上有效索引

我有一个查询,旨在循环和搜索重复地址,该查询使用 REGEX_REPLACE。我正在尝试在正则表达式上建立索引,就像进行解释一样,并且它使用正则表达式上的过滤器对 user_property 表进行顺序扫描

EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS)  with user_detail AS (
        SELECT user_id,
            max(user_property_value) FILTER (WHERE user_property_type_id = 6 ) AS FIRST_NAME,
            max(user_property_value) FILTER (WHERE user_property_type_id = 7 ) AS LAST_NAME,
            max(TO_DATE(user_property_value, 'YYYY-MM-DD')) FILTER (WHERE user_property_type_id = 8 ) AS DOB,
            max(user_property_value) FILTER (WHERE user_property_type_id = 33 ) AS BIRTH_NUMBER
        FROM PUBLIC.user_property cp
        JOIN PUBLIC.user c using (user_id)
        WHERE c.user_group_id= '38'
        AND cp.user_property_is_active
        GROUP BY user_id
    ),
    duplicate as (
        SELECT COALESCE(MAX(
                CASE WHEN REGEXP_REPLACE((address_line1), E'\\_|\\W','','g') = 'Flat …
Run Code Online (Sandbox Code Playgroud)

postgresql index regular-expression regex

5
推荐指数
1
解决办法
659
查看次数

如何优化两个索引列上的 PostgreSQL OR 查询

我有一个大型分区表,用于存储帐户之间的货币交易。

CREATE TABLE "transactions" (
  "from" BYTEA        NOT NULL  -- sender account
  ,"to" BYTEA         NOT NULL  -- receiver account
  ,"type" INTEGER     NOT NULL  -- type of transfer
  ,"ts" TIMESTAMP     NOT NULL  -- timestamp of transfer
) PARTITION BY RANGE ("ts");
Run Code Online (Sandbox Code Playgroud)

“transactions”在“ts”、“from”和“to”上有索引(“ts”用于排序)。

CREATE INDEX "transactions_ts_idx"   ON "transactions" USING BTREE ("ts");
CREATE INDEX "transactions_from_idx" ON "transactions" USING BTREE ("from", "ts");
CREATE INDEX "transactions_to_idx"   ON "transactions" USING BTREE ("to",   "ts");
Run Code Online (Sandbox Code Playgroud)

我想查询涉及给定帐户或没有帐户的所有交易,例如:

-- given an account <account>
SELECT * FROM "transactions"
WHERE "from" = …
Run Code Online (Sandbox Code Playgroud)

postgresql index execution-plan index-tuning query-performance

5
推荐指数
1
解决办法
376
查看次数

如何匹配 pg_repack 二进制文件和数据库版本

我正在使用此命令 sudo apt-get install postgresql-11-repack来安装 pg_repack(默认为 1.4.8)。

postgresql-11-repack is already the newest version (1.4.8-1.pgdg20.04+1).
Run Code Online (Sandbox Code Playgroud)

当我在 rds postgres 11 中运行“创建扩展 pg_repack”时,它默认使用版本 1.4.4 创建。

                                        List of installed extensions
     Name     | Version |   Schema   |                         Description
--------------+---------+------------+--------------------------------------------------------------
 pg_repack    | 1.4.4   | public     | Reorganize tables in PostgreSQL databases with minimal locks
Run Code Online (Sandbox Code Playgroud)

因此 pg_repack 由于 ec2 和 db 之间的版本号不匹配而失败。

ERROR: pg_repack failed with error: program 'pg_repack 1.4.8' does not match database library 'pg_repack 1.4.4'
Run Code Online (Sandbox Code Playgroud)
  1. 如何在数据库中创建版本 1.4.8 的重新打包扩展?
  2. 如果上述方法不可行,我如何将 ec2 上的 pg_repack 版本降级回与我的数据库相同的 1.4.4。 …

postgresql debian postgresql-extensions

5
推荐指数
2
解决办法
1345
查看次数

同时聚合计数和完整计数

我有一个events这样的表:

create table events
(
    correlation_id char(26) not null,
    user_id        bigint,
    task_id        bigint not null,
    location_id    bigint,
    type           bigint not null,
    created_at     timestamp(6) with time zone not null,
    constraint events_correlation_id_created_at_user_id_unique
        unique (correlation_id, created_at, user_id)
);
Run Code Online (Sandbox Code Playgroud)
CREATE TABLE
Run Code Online (Sandbox Code Playgroud)

该表保存正在执行的任务的记录,如下所示:

insert into events (correlation_id, user_id, task_id, location_id, type, created_at)
values  ('01CN4HP4AN0000000000000001', 4, 58, 30, 0, '2018-08-17 18:17:15.348629+00'),
        ('01CN4HP4AN0000000000000001', 4, 58, 30, 1, '2018-08-17 18:17:22.852299+00'),
        ('01CN4HP4AN0000000000000001', 4, 58, 30, 99, '2018-08-17 18:17:25.535593+00'),
        ('01CN4J9SZ80000000000000003', 4, 97, 30, 0, '2018-08-17 18:28:00.104093+00'),
        ('01CN4J9SZ80000000000000003', 4, …
Run Code Online (Sandbox Code Playgroud)

postgresql aggregate count

5
推荐指数
1
解决办法
291
查看次数

离线优先数据库设计如何工作?

我想制作一个离线优先的待办事项应用程序,它可以在移动应用程序(SQLite)和网络应用程序(PostgreSQL)上运行。该应用程序将具有以下商业模式:

用户:

  • ID
  • 姓名
  • 创建日期时间

去做:

  • ID
  • 用户身份
  • 标题
  • 完毕
  • 创建日期时间
  • 更新日期时间

创建新用户:

  • 只能使用网络应用程序创建新用户。

创建新的待办事项:

  • 可以使用网络应用程序创建待办事项。
  • 如果移动应用程序在线,请将待办事项发布到网络应用程序上。id如果帖子成功,请在移动应用程序中创建具有相同成功待办事项的待办事项。
  • 如果移动应用程序处于离线状态,只要网络应用程序再次上线,仍会创建新的待办事项,并将新的待办事项更新/发布到网络应用程序。

如果手机处于离线状态并且我没有任何 ID,我不确定如何创建新的待办事项。如果我为移动应用程序创建一个虚拟 ID,它可能会与其他待办事项 ID 发生冲突。或者我应该在移动应用程序中使用 UUID 作为我所有待办事项的 ID,然后将其发布到网络应用程序?其他离线优先应用程序数据库设计如何工作?

postgresql sqlite offline

5
推荐指数
1
解决办法
957
查看次数

postgresql 匹配或转换 utf-8 变体字符串

Postgres 13

我正在寻找一种在 postgresql 中搜索可能具有变体字符表示形式的 UTF-8 文本的方法(正确的术语是什么?即vs )。life

我遇到匹配变体字符的问题,请考虑

-- This works as expected
select 'life' ilike '%life%' matches 

-- I would like to also be able to match against this source text like this
select '' ilike '%life%' matches
Run Code Online (Sandbox Code Playgroud)

请注意,有无数的变体,我此时并不特别关心与非 ascii 可表示字符的匹配,也就是说,我认为只要最终与 匹配,我就可以从 utf-8 到 ascii 有损转换life

postgresql collation utf-8 string-searching postgresql-13

5
推荐指数
1
解决办法
583
查看次数

Linux 和 Mac 上 PostgreSQL 14 排序规则行为的差异

我在 Mac 和 Ubuntu 服务器上安装了 PostgreSQL,如下所示:

atsweb=# select version();
                                                      version                                                      
-------------------------------------------------------------------------------------------------------------------
 PostgreSQL 14.6 on x86_64-apple-darwin20.6.0, compiled by Apple clang version 12.0.0 (clang-1200.0.32.29), 64-bit
(1 row)
Run Code Online (Sandbox Code Playgroud)
atsweb=# select version();
                                                               version                                                                
--------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 14.6 (Ubuntu 14.6-0ubuntu0.22.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0, 64-bit
(1 row)
Run Code Online (Sandbox Code Playgroud)

它们具有相同的数据库、排序规则和编码:

atsweb=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 atsweb    | atsweb   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | …
Run Code Online (Sandbox Code Playgroud)

postgresql collation ubuntu mac-os-x

5
推荐指数
2
解决办法
1080
查看次数

将时间戳列更改为时间戳而不“转换”数据?

似乎将列从 更改为timestamp without time zonetimestamp with time zone根据语句时的当前会话时区转换现有数据alter

\n

请参阅此示例,每个语句后显示输出

\n
create table tztest (col1 timestamp without time zone);\n\nset timezone = 'UTC';\ninsert into tztest (col1) values ('2023-02-01 10:10:10');\nselect col1 as t1, extract(epoch FROM col1) from tztest;\n-- \xe2\x86\x92 2023-02-01 10:10:10    1675246210\n\nset timezone = 'America/New_York';\nalter table tztest alter column col1 type timestamp with time zone;\nselect col1 as t2, extract(epoch FROM col1) from tztest;\n-- \xe2\x86\x92 2023-02-01 10:10:10-05 1675264210\n\nset timezone = 'UTC';\nselect col1 as t3, extract(epoch FROM col1) …
Run Code Online (Sandbox Code Playgroud)

postgresql alter-table timezone

5
推荐指数
1
解决办法
2144
查看次数

这些单字母 SQL 关键字是什么?

我在这里查看 SQL 关键字列表https://www.postgresql.org/docs/current/sql-keywords-appendix.html

那里有一些单字母关键字。我找不到有关他们的任何信息。我在哪里可以找到这些信息以及这些信息的用途是什么?

例子, ACFGKMPT

postgresql syntax sql-standard

5
推荐指数
1
解决办法
118
查看次数

如何确定 PostgreSQL 中索引的排序规则?

在 PostgreSQL 15 中,当使用隐式排序规则创建索引时,会记录用于该索引的默认排序规则:

默认情况下,索引使用为要索引的列声明的排序规则或要索引的表达式的结果排序规则。具有非默认排序规则的索引对于涉及使用非默认排序规则的表达式的查询非常有用。

但是,在使用默认排序规则(如文档中所述)在列上创建索引后,我们可以更改列的排序规则。根据我的理解,更改列的排序规则不应更改已创建索引的排序规则。

因此,在任何时间点,我们都应该能够查询可能与相应列排序规则不同的索引的实际排序规则。

我们怎样才能做到这一点?

我做了一些测试,但并没有得出结论:

psql (15.3)
Type "help" for help.

postgres=# SELECT version();
                                   version                                    
------------------------------------------------------------------------------
 PostgreSQL 15.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 12.2.0, 64-bit
(1 row)

postgres=# CREATE DATABASE test10
TEMPLATE = template0
LOCALE = "C"
ENCODING = 'UTF8';
CREATE DATABASE
postgres=# \c test10
You are now connected to database "test10" as user "postgres".
test10=# CREATE TABLE public.asset(
        asset_id uuid NOT NULL,
        name text NOT NULL,
        CONSTRAINT pk_asset PRIMARY KEY (asset_id)
); …
Run Code Online (Sandbox Code Playgroud)

postgresql collation postgresql-15

5
推荐指数
1
解决办法
675
查看次数