<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr">
	<id>https://knowledge.pinon-hebert.fr/mediawiki/index.php?action=history&amp;feed=atom&amp;title=POSTGRES_V%C3%A9rifier_les_s%C3%A9quences_d%27un_sch%C3%A9ma</id>
	<title>POSTGRES Vérifier les séquences d&#039;un schéma - Historique des versions</title>
	<link rel="self" type="application/atom+xml" href="https://knowledge.pinon-hebert.fr/mediawiki/index.php?action=history&amp;feed=atom&amp;title=POSTGRES_V%C3%A9rifier_les_s%C3%A9quences_d%27un_sch%C3%A9ma"/>
	<link rel="alternate" type="text/html" href="https://knowledge.pinon-hebert.fr/mediawiki/index.php?title=POSTGRES_V%C3%A9rifier_les_s%C3%A9quences_d%27un_sch%C3%A9ma&amp;action=history"/>
	<updated>2026-05-19T14:55:03Z</updated>
	<subtitle>Historique des versions pour cette page sur le wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://knowledge.pinon-hebert.fr/mediawiki/index.php?title=POSTGRES_V%C3%A9rifier_les_s%C3%A9quences_d%27un_sch%C3%A9ma&amp;diff=1855&amp;oldid=prev</id>
		<title>Jpinon le 27 janvier 2026 à 16:19</title>
		<link rel="alternate" type="text/html" href="https://knowledge.pinon-hebert.fr/mediawiki/index.php?title=POSTGRES_V%C3%A9rifier_les_s%C3%A9quences_d%27un_sch%C3%A9ma&amp;diff=1855&amp;oldid=prev"/>
		<updated>2026-01-27T16:19:04Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;fr&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Version précédente&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Version du 27 janvier 2026 à 16:19&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l60&quot;&gt;Ligne 60 :&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Ligne 60 :&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;C&amp;#039;est &amp;#039;&amp;#039;copilot&amp;#039;&amp;#039; qui est arrivé à pondre ça après 4 ou 5 essais infructueux.  &lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;C&amp;#039;est &amp;#039;&amp;#039;copilot&amp;#039;&amp;#039; qui est arrivé à pondre ça après 4 ou 5 essais infructueux.  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Le résultat est quelque chose comme :&amp;lt;syntaxhighlight lang=&quot;text&quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;On appelle la fonction avec :&amp;lt;syntaxhighlight lang=&quot;postgresql&quot;&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;SELECT * FROM check_sequences(&#039;monschema&#039;);&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/ins&gt;Le résultat est quelque chose comme :&amp;lt;syntaxhighlight lang=&quot;text&quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;user_role	id	user_role_id_seq	1	2	NOK&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;user_role	id	user_role_id_seq	1	2	NOK&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;user_list	id	prd_user_list_id_seq	382	382	OK&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;user_list	id	prd_user_list_id_seq	382	382	OK&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l81&quot;&gt;Ligne 81 :&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Ligne 84 :&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;application	id	prd_application_id_seq	93	93	OK&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;application	id	prd_application_id_seq	93	93	OK&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;status_value	id	prd_status_value_id_seq	11	11	OK&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;status_value	id	prd_status_value_id_seq	11	11	OK&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;On pourait envisager un script qui fasse les mises à jours automatiquement... à voir.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Jpinon</name></author>
	</entry>
	<entry>
		<id>https://knowledge.pinon-hebert.fr/mediawiki/index.php?title=POSTGRES_V%C3%A9rifier_les_s%C3%A9quences_d%27un_sch%C3%A9ma&amp;diff=1854&amp;oldid=prev</id>
		<title>Jpinon : sequences postgres</title>
		<link rel="alternate" type="text/html" href="https://knowledge.pinon-hebert.fr/mediawiki/index.php?title=POSTGRES_V%C3%A9rifier_les_s%C3%A9quences_d%27un_sch%C3%A9ma&amp;diff=1854&amp;oldid=prev"/>
		<updated>2026-01-27T16:01:44Z</updated>

		<summary type="html">&lt;p&gt;sequences postgres&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Nouvelle page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Souvent on a des &amp;quot;inserts&amp;quot; dans une base qui plantent à cause de la contrainte d&amp;#039;unicité d&amp;#039;une table. Plus particulièrement quand on a un id que dépends d&amp;#039;une séquence.&lt;br /&gt;
&lt;br /&gt;
On utilise pour cela la fonction suivante :&amp;lt;syntaxhighlight lang=&amp;quot;postgresql&amp;quot;&amp;gt;&lt;br /&gt;
CREATE OR REPLACE FUNCTION check_sequences(p_schema text)&lt;br /&gt;
RETURNS TABLE(&lt;br /&gt;
    table_name text,&lt;br /&gt;
    column_name text,&lt;br /&gt;
    sequence_name text,&lt;br /&gt;
    seq_value bigint,&lt;br /&gt;
    max_value bigint,&lt;br /&gt;
    status text&lt;br /&gt;
)&lt;br /&gt;
LANGUAGE plpgsql&lt;br /&gt;
AS $$&lt;br /&gt;
DECLARE&lt;br /&gt;
    rec record;&lt;br /&gt;
    dyn_sql text;&lt;br /&gt;
    max_val bigint;&lt;br /&gt;
    last_val bigint;&lt;br /&gt;
BEGIN&lt;br /&gt;
    FOR rec IN&lt;br /&gt;
        SELECT&lt;br /&gt;
            t.relname AS table_name,&lt;br /&gt;
            a.attname AS column_name,&lt;br /&gt;
            s.relname AS sequence_name,&lt;br /&gt;
            n.nspname AS seq_schema&lt;br /&gt;
        FROM pg_class t&lt;br /&gt;
        JOIN pg_namespace n2 ON n2.oid = t.relnamespace&lt;br /&gt;
        JOIN pg_attribute a ON a.attrelid = t.oid&lt;br /&gt;
        JOIN pg_attrdef ad ON ad.adrelid = t.oid AND ad.adnum = a.attnum&lt;br /&gt;
        JOIN pg_depend d ON d.refobjid = t.oid AND d.refobjsubid = a.attnum&lt;br /&gt;
        JOIN pg_class s ON s.oid = d.objid AND s.relkind = &amp;#039;S&amp;#039;&lt;br /&gt;
        JOIN pg_namespace n ON n.oid = s.relnamespace&lt;br /&gt;
        WHERE t.relkind = &amp;#039;r&amp;#039;&lt;br /&gt;
          AND n2.nspname = p_schema&lt;br /&gt;
    LOOP&lt;br /&gt;
        -- Lire last_value de la séquence&lt;br /&gt;
        EXECUTE format(&amp;#039;SELECT last_value FROM %I.%I&amp;#039;, rec.seq_schema, rec.sequence_name)&lt;br /&gt;
            INTO last_val;&lt;br /&gt;
&lt;br /&gt;
        -- Lire MAX(colonne) de la table&lt;br /&gt;
        dyn_sql := format(&lt;br /&gt;
            &amp;#039;SELECT COALESCE(MAX(%I), 0) FROM %I.%I&amp;#039;,&lt;br /&gt;
            rec.column_name, p_schema, rec.table_name&lt;br /&gt;
        );&lt;br /&gt;
        EXECUTE dyn_sql INTO max_val;&lt;br /&gt;
&lt;br /&gt;
        -- Assigner aux OUT&lt;br /&gt;
        table_name := rec.table_name;&lt;br /&gt;
        column_name := rec.column_name;&lt;br /&gt;
        sequence_name := rec.sequence_name;&lt;br /&gt;
        seq_value := last_val;&lt;br /&gt;
        max_value := max_val;&lt;br /&gt;
        status := CASE WHEN last_val &amp;lt; max_val THEN &amp;#039;NOK&amp;#039; ELSE &amp;#039;OK&amp;#039; END;&lt;br /&gt;
&lt;br /&gt;
        RETURN NEXT;&lt;br /&gt;
    END LOOP;&lt;br /&gt;
END;&lt;br /&gt;
$$;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;C&amp;#039;est &amp;#039;&amp;#039;copilot&amp;#039;&amp;#039; qui est arrivé à pondre ça après 4 ou 5 essais infructueux. &lt;br /&gt;
&lt;br /&gt;
Le résultat est quelque chose comme :&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
user_role	id	user_role_id_seq	1	2	NOK&lt;br /&gt;
user_list	id	prd_user_list_id_seq	382	382	OK&lt;br /&gt;
user_alert	id	user_alert_id_seq	7	7	OK&lt;br /&gt;
application	id	prd_application_id_seq	93	93	OK&lt;br /&gt;
status_value	id	prd_status_value_id_seq	10	11	NOK&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Tous les NOK (not OK) indiquent une séquence pas à jours.&lt;br /&gt;
&lt;br /&gt;
On fait les mises à jour:&amp;lt;syntaxhighlight lang=&amp;quot;postgresql&amp;quot;&amp;gt;&lt;br /&gt;
ALTER SEQUENCE spoc_rec.user_role_id_seq&lt;br /&gt;
	RESTART 2;&lt;br /&gt;
&lt;br /&gt;
ALTER SEQUENCE prd_status_value_id_seq&lt;br /&gt;
	RESTART 11;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Tout revient dans l&amp;#039;ordre:&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
user_role	id	user_role_id_seq	2	2	OK&lt;br /&gt;
user_spoc	id	prd_user_spoc_id_seq	382	382	OK&lt;br /&gt;
user_alert	id	user_alert_id_seq	7	7	OK&lt;br /&gt;
application	id	prd_application_id_seq	93	93	OK&lt;br /&gt;
status_value	id	prd_status_value_id_seq	11	11	OK&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jpinon</name></author>
	</entry>
</feed>