GithubHelp home page GithubHelp logo

df7cb / pg_dirtyread Goto Github PK

View Code? Open in Web Editor NEW
125.0 10.0 25.0 99 KB

Read dead but unvacuumed tuples from a PostgreSQL relation

License: Other

Makefile 1.25% C 50.92% PLpgSQL 47.79% Vim Script 0.04%
pg-dirtyread postgresql extension uncommitted

pg_dirtyread's Introduction

pg_dirtyread

The pg_dirtyread extension provides the ability to read dead but unvacuumed rows from a relation. Supports PostgreSQL 9.2 and later. (On 9.2, at least 9.2.9 is required.)

Building

To build pg_dirtyread, just do this:

make
make install

If you encounter an error such as:

make: pg_config: Command not found

Be sure that you have pg_config installed and in your path. If you used a package management system such as RPM to install PostgreSQL, be sure that the -devel package is also installed. If necessary tell the build process where to find it:

make PG_CONFIG=/path/to/pg_config
make install PG_CONFIG=/path/to/pg_config

Loading and Using

Once pg_dirtyread is built and installed, you can add it to a database. Loading pg_dirtyread is as simple as connecting to a database as a super user and running:

  CREATE EXTENSION pg_dirtyread;
  SELECT * FROM pg_dirtyread('tablename') AS t(col1 type1, col2 type2, ...);

The pg_dirtyread() function returns RECORD, therefore it is necessary to attach a table alias clause that describes the table schema. Columns are matched by name, so it is possible to omit some columns in the alias, or rearrange columns.

Example:

  CREATE EXTENSION pg_dirtyread;

  -- Create table and disable autovacuum
  CREATE TABLE foo (bar bigint, baz text);
  ALTER TABLE foo SET (
    autovacuum_enabled = false, toast.autovacuum_enabled = false
  );

  INSERT INTO foo VALUES (1, 'Test'), (2, 'New Test');
  DELETE FROM foo WHERE bar = 1;

  SELECT * FROM pg_dirtyread('foo') as t(bar bigint, baz text);
   bar │   baz
  ─────┼──────────
     1 │ Test
     2 │ New Test

Dropped Columns

The content of dropped columns can be retrieved as long as the table has not been rewritten (e.g. via VACUUM FULL or CLUSTER). Use dropped_N to access the Nth column, counting from 1. PostgreSQL deletes the type information of the original column, so only a few sanity checks can be done if the correct type was specified in the table alias; checked are type length, type alignment, type modifier, and pass-by-value.

  CREATE TABLE ab(a text, b text);
  INSERT INTO ab VALUES ('Hello', 'World');
  ALTER TABLE ab DROP COLUMN b;
  DELETE FROM ab;
  SELECT * FROM pg_dirtyread('ab') ab(a text, dropped_2 text);
     a   │ dropped_2
  ───────┼───────────
   Hello │ World

System Columns

System columns such as xmax and ctid can be retrieved by including them in the table alias attached to the pg_dirtyread() call. A special column dead of type boolean is available to report dead rows (as by HeapTupleIsSurelyDead). The dead column is not usable during recovery, i.e. most notably not on standby servers. The oid column is only available in PostgreSQL version 11 and earlier.

  SELECT * FROM pg_dirtyread('foo')
      AS t(tableoid oid, ctid tid, xmin xid, xmax xid, cmin cid, cmax cid, dead boolean,
           bar bigint, baz text);
   tableoid │ ctid  │ xmin │ xmax │ cmin │ cmax │ dead │ bar │        baz
  ──────────┼───────┼──────┼──────┼──────┼──────┼──────┼─────┼───────────────────
      41823 │ (0,1) │ 1484148500 │ t    │   1Delete
      41823 │ (0,2) │ 1484000 │ f    │   2 │ Insert
      41823 │ (0,3) │ 1484148600 │ t    │   3Update
      41823 │ (0,4) │ 1484148800 │ f    │   4 │ Not deleted
      41823 │ (0,5) │ 1484148911 │ f    │   5 │ Not updated
      41823 │ (0,6) │ 1486000 │ f    │   3 │ Updated
      41823 │ (0,7) │ 1489011 │ t    │   5 │ Not quite updated
      41823 │ (0,8) │ 1490022 │ t    │   6 │ Not inserted

Authors

pg_dirtyread 1.0 was written by Phil Sorber in 2012. Christoph Berg added the ability to retrieve system columns in version 1.1, released 2017, and took over further maintenance.

License

Copyright (c) 1996-2024, PostgreSQL Global Development Group

Copyright (c) 2012, OmniTI Computer Consulting, Inc.

Portions Copyright (c) 1994, The Regents of the University of California

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name OmniTI Computer Consulting, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pg_dirtyread's People

Contributors

df7cb avatar dthadi3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pg_dirtyread's Issues

Could not open extension control file

I am using MacOS Monterey and PostgreSQL 14. I am getting the following error when I run CREATE EXTENSION pg_dirtyread;:

[58P01] ERROR: could not open extension control file "/rdsdbbin/postgres-14.4.R1/share/extension/pg_dirtyread.control": No such file or directory

Any idea why can this be? Installation with make went on successfully.

Thanks a lot!

Getting error converting tuple descriptors

Hello, I'm trying to execute this query:

SELECT * FROM pg_dirtyread('pg_catalog.pg_constraint') 
   as (oid oid, conname name, connamespace oid, contype char);

But I'm getting the following error:

SQL Error [42804]: ERROR: Error converting tuple descriptors!
Detail: Attribute "contype" has type "char" in corresponding attribute of type pg_constraint.

I'm using Postgres 12.11 for Ubuntu.

Seems like an error converting the type char...
Any ideas or solutions?

Thanks;

LOG: server process (PID 30035) was terminated by signal 11: Segmentation fault

After executing postgres@pybossa LOG: statement: select * from pg_dirtyread(...)
I get the following error in my logs:
2021-01-16 22:54:07.403 EET [29991] LOG: server process (PID 30035) was terminated by signal 11: Segmentation fault
2021-01-16 22:54:07.403 EET [29991] LOG: terminating any other active server processes
2021-01-16 22:54:07.405 EET [30036] postgres@pybossa FATAL: the database system is in recovery mode
2021-01-16 22:54:07.408 EET [29991] LOG: all server processes terminated; reinitializing

I read that this could be a RAM issue ... maybe because the table contains too many records (more than 10k and some of those are decently sized jsonb columns). I was wondering if you would know If I can backup the database and run pg_dirtyread() on a better machine. My issue is that I am not very familiar with the how the pg_dump works and if the unvacummed rows would be kept during the process.

I would really love some help on this :) Lost some important data :(

GetOldestXmin() is not allowed on a hot standby

smith@moll:~$ psql -U smith -p 54331 -h /tmp regression
psql (9.6.3, server 10beta2)
regression=> select * from pg_dirtyread('tenk1'::regclass) t(dead boolean, ctid tid);
server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.
smith@moll:~$ grep TRAP log-slave
TRAP: FailedAssertion("!(allDbs || !RecoveryInProgress())", File: "procarray.c", Line: 1332)

Why can't I look at the example and see [baz] column: delete update ?

[pg12@client1 extension]$ psql -V
psql (PostgreSQL) 12.7
[pg12@client1 extension]$

pg_dirtyread-2.2

###################################

mydb=# \d t1
Table "public.t1"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
id | integer | | |
name | character varying(10) | | |

mydb=# select * from pg_dirtyread('t1') as recovery_t(id int,name varchar(10));
id | name
----+------
4 | d
3 | cc
3 | cc
(3 rows)

mydb=# SELECT * FROM pg_dirtyread('t1')
mydb-# AS t(tableoid oid, ctid tid, xmin xid, xmax xid, cmin cid, cmax cid, dead boolean,
mydb(# id int, name varchar(10));
tableoid | ctid | xmin | xmax | cmin | cmax | dead | id | name
----------+-------+------+------+------+------+------+----+------
16386 | (0,1) | 493 | 500 | 0 | 0 | t | 4 | d
16386 | (0,2) | 497 | 0 | 0 | 0 | t | 3 | cc
16386 | (0,3) | 498 | 0 | 0 | 0 | t | 3 | cc
(3 rows)

----- Why can't I look at the example and see [baz] column: delete update ?

SELECT * FROM pg_dirtyread('foo')
AS t(tableoid oid, ctid tid, xmin xid, xmax xid, cmin cid, cmax cid, dead boolean,
bar bigint, baz text);
tableoid │ ctid │ xmin │ xmax │ cmin │ cmax │ dead │ bar │ baz
──────────┼───────┼──────┼──────┼──────┼──────┼──────┼─────┼───────────────────
41823 │ (0,1) │ 1484 │ 1485 │ 0 │ 0 │ t │ 1 │ Delete
41823 │ (0,2) │ 1484 │ 0 │ 0 │ 0 │ f │ 2 │ Insert
41823 │ (0,3) │ 1484 │ 1486 │ 0 │ 0 │ t │ 3 │ Update

extension fails on Postgresql 9.2.4: Undefined symbol "heap_copy_tuple_as_datum"

pg924db=# create  EXTENSION pg_dirtyread;
ERROR:  could not load library "/usr/local/lib/postgresql/pg_dirtyread.so": dlopen (/usr/local/lib/postgresql/pg_dirtyread.so) failed: /usr/local/lib/postgresql/pg_dirtyread.so: Undefined symbol "heap_copy_tuple_as_datum"

gmake

...
pg_dirtyread.c: In function 'pg_dirtyread':
pg_dirtyread.c:140: warning: implicit declaration of function 'heap_copy_tuple_as_datum'
....

pg_config --version

PostgreSQL 9.2.4

when I execute 'make' to build pg_dirtyread,I met a question 'gcc: fatal error: cannot specify ‘-o’ with ‘-c’, ‘-S’ or ‘-E’ with multiple files'

[root@iZ2vc2iy7g5tupvfss5hdkZ pg_dirtyread-master]# make
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -fPIC -fvisibility=hidden /data/postgresql/postgresql-16.3/src/include/postgres.h -I. -I./ -I/usr/include/pgsql/server -I/usr/include/pgsql/internal -D_GNU_SOURCE -c -o pg_dirtyread.o pg_dirtyread.c
gcc: fatal error: cannot specify ‘-o’ with ‘-c’, ‘-S’ or ‘-E’ with multiple files
compilation terminated.
make: *** [: pg_dirtyread.o] Error 1

you know why?

Version match problem

ERROR: incompatible library "/usr/pgsql-9.6/lib/pg_dirtyread.so": version mismatch
DETAIL: Server is version 9.6, library is version 9.2.
How to resolve this issue?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.