Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Postgres是否正在缓存我们的查询?我们如何绕过它?_Python_Postgresql_Concurrency_Transactions_Postico - Fatal编程技术网

Python Postgres是否正在缓存我们的查询?我们如何绕过它?

Python Postgres是否正在缓存我们的查询?我们如何绕过它?,python,postgresql,concurrency,transactions,postico,Python,Postgresql,Concurrency,Transactions,Postico,我正在尝试运行以下python3代码: import os import psycopg2 import logging # Set max attempts before giving up MAX_ATTEMPTS = 5 # Set basic logging config to debug (i.e. log everything). # By default, this will log te stdout (i.e. it will behave the same as prin

我正在尝试运行以下python3代码:

import os
import psycopg2
import logging

# Set max attempts before giving up
MAX_ATTEMPTS = 5

# Set basic logging config to debug (i.e. log everything).
# By default, this will log te stdout (i.e. it will behave the same as print)
logging.basicConfig(level=logging.DEBUG)

# Grab DB url from env variable
database_url = os.environ.get('DATABASE_URL')

assert database_url is not None, 'DATABASE_URL env variable must be set to a postgres connection string.'

# Initiate psycopg2 and instantiate a cursor object
conn = psycopg2.connect(database_url)
cursor = conn.cursor()


# Define function to delete old records
def delete_old_records(cur):
    # execute a query to delete old records. We're going to refer to this as the "delete" command
    query = 'DELETE FROM my_table WHERE id NOT IN ( SELECT id FROM ( SELECT id FROM my_table ORDER BY id DESC LIMIT 1850 ) foo);'
    cur.execute(query)


# Set variables to keep track of loop
successful = False
attempts = 0

# While not successful and max attempts not reached
while not successful and attempts < MAX_ATTEMPTS:
    try:
        # Attempt to delete old records
        delete_old_records(cursor)
        # Set successful to True if no errors were encountered in the previous line
        successful = True
        # Log a message
        logging.info('Successfully truncated old records!')
    # If some psycopg2 error happens
    except psycopg2.Error as e:
        # Log the error
        logging.exception('Got exception when executing query')
        # Rollback the cursor and get ready to try again
        conn.rollback()
        # Increment attempts by 1
        attempts += 1

# If not able to perform operation after max attempts, log message to indicate failure
if not successful:
    logging.warning(f'Was not successfully able to truncate logs after {MAX_ATTEMPTS} retries. '
                    f'Check logs for traceback (console output by default).')
我们得到1860而不是1850(即,行未被删除)

  • 在psql或postico中手动运行delete命令时,我们分别在psql或postico中运行COUNT命令时得到正确的结果。但是,在ipython中运行命令时,我们会得到不同的结果

  • 当我打开与计算机A上ipython上的db的连接,并运行delete命令,然后打开另一个与计算机B上ipython上的db的连接器并运行count命令时,我发现db行计数没有改变,即仍然是1860,没有切到1850


  • 我怀疑缓存/备忘录化,但我不确定我的命令是否真的有效。psycopg2、Postco或postgres本身是否有可能导致这种情况?我们该如何应对呢?我们在Postco或psycopg2/postgres上看不到任何清除缓存

    不涉及缓存。PostgreSQL不缓存查询结果


    您只是忘记了提交删除事务,因此它的效果在任何并发事务中都不可见。

    我尝试了执行connection.COMMIT()和connection as conn:with conn.cursor()as cur:cur.execute(query),但Postco仍然将count查询的结果显示为1860@劳伦斯:没关系,我和往常一样是个白痴。错误的数据库连接字符串。谢谢!你能详细说明一下你的答案吗?什么数据库引擎有或没有缓存,以及缓存是如何工作的?我只知道MySQL有一个查询缓存,但我认为这是一个不合适的功能,在99%的情况下最好关闭它。我不知道它是怎么工作的。
    SELECT count(*) from my_table;